How do I prevent peak clipping when using audiowrite?
36 views (last 30 days)
Show older comments
Hi,
I'm new to using Matlab. I have something like this:
%Takes wav file and reads it
[y,fs]=audioread('sampleaudio.wav')
%Runs it through hearing aid function (I have this already programmed)
y = WDRC(y, fs)
%creates new wav file
audiowrite('sampleaudioWDRC.wav',y,fs)
The problem is I keep getting this error
Warning: Data clipped when writing file.
> In audiowrite>clipInputData at 389
In audiowrite at 167
How do I fix this? Any help would be appreciated, thanks!
0 Comments
Answers (4)
Joseph Cheng
on 28 Jul 2014
What is the max and min of your y after your hearing aid function and what was it before the hearing aid function? you may be writing values out of the -1 and 1 range for signed single class numbers. Perhapse normalizing it to the maximum range for the class that your y is in.
0 Comments
Star Strider
on 28 Jul 2014
The audiowrite function may not be your only option. See the documentation for Export to Audio Files for more information.
1 Comment
Star Strider
on 28 Jul 2014
Edited: Star Strider
on 28 Jul 2014
It suggests you use the save function instead of wavwrite to avoid the clipping problem. Use load (link at the end of the save page) to load them back into your workspace. If you specifically save your data as:
save('sampleaudio.mat', 'y', 'fs')
you can then load it as:
load('sampleaudio.mat')
in any MATLAB script or function, and y and fs will be in the workspace of that script or function.
There are variations of both the save and load functions you can experiment with if you want to.
Andrea Genovese
on 6 Apr 2018
You have to normalize (limit the range between -1 to +1)
y = y/max(abs(y)) % for mono
y = y/max(abs(y(:))) % for stereo
You can also then decide to leave more headroom for the problem of true-peak in poor DAC system
y = y * 0.9 % Now it will peak at a value of 0.9
1 Comment
Walter Roberson
on 6 Apr 2018
Note:
The older wavwrite() had slightly different rules:
-1 <= x < 1
whereas audiowrite has the rule
-1 <= x <= 1
With wavwrite(), using
y/max(abs(y))
could leave you with +1 exactly if the maximum happened to be positive, so for that older wavwrite, it would be preferred to use
y / max(abs(y)) * (1-eps)
This is equivalent to using a "headroom" of eps in what Andrea wrote.
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!