# why is wavwrite clipping my data

8 views (last 30 days)
tony karamp on 28 Mar 2013
Hello all,
I have this sinwave and I want to save it as a wav file. I use the wavwrite function but when I run the script I get a warning saying that the data has been clipped during write to file.
Is there a way to scale everything like the function soundsc does?
Thanks a bunch...

Jan on 28 Mar 2013
Whenever you get a problem with a command in Matlab, it is recommended to read the intructions:
doc wavwrite
There you find, that the range is limited to –1.0 <= y < +1.0 for 8, 16 and 24 bits, while +1.0 is allowed for 32 bit only. Or if you have integer data (ayour see, it would be helpfujl if you explain this detail in your question), there are other ranges also.

Wayne King on 28 Mar 2013
Edited: Wayne King on 28 Mar 2013
the values in your sine wave likely exceed [-1, 1] or come so close to [-1, 1] that they are 1 for all intents and purposes. The are a number of ways to work around this.
%assuming the sine wave is zero mean
t = 0:0.001:1-0.001;
x = 2*cos(2*pi*100*t);
[val,idx] = max(abs(x));
x = x./(max(abs(x))+0.01);
wavwrite(x,1000,16,'test.wav');
If it's not zero mean then first subtract the mean
x = 2+cos(2*pi*100*t);
x = x-mean(x);
x = x./(max(abs(x))+0.01);