How do I prevent peak clipping when using audiowrite?
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(4 个)
Joseph Cheng
2014-7-28
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 个评论
Star Strider
2014-7-28
The audiowrite function may not be your only option. See the documentation for Export to Audio Files for more information.
1 个评论
Star Strider
2014-7-28
编辑:Star Strider
2014-7-28
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
2018-4-6
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 个评论
Walter Roberson
2018-4-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!