Why do I get additional frequencies using standard audiowrite BitsPerSample values?

2 views (last 30 days)
Hello,
I want to write spectral data to wav file and read it again.
If I do some example with two sine waves, after writing+reading a wav file I get more frequencies in spectrum (aliasing/quantization noise?).
% generate sin-waves
wave = dsp.SineWave(Frequency=2.45e6,SampleRate=5e6);
wave.SamplesPerFrame = 1024;
wave2 = dsp.SineWave(Frequency=2.46e6,SampleRate=5e6);
wave2.SamplesPerFrame = 1024;
% save and visualize
scope1 = spectrumAnalyzer(SampleRate=wave.SampleRate,...
ViewType="spectrum-and-spectrogram",...
PlotAsTwoSidedSpectrum=false);
%data = [];
y = [];
for ii = 1:250
x = wave() + 0.5*wave2()+ 0.05*randn(1024,1);
scope1(x);
pause(0.01)
if scope1.isNewDataReady
y = [y,x];
%data = [data;getSpectrumData(scope)];
end
end
% set
Fs = 48000;
% write wav
audiowrite(wavefile,y,Fs,'BitsPerSample',16);
% visualize output orig
scope2 = spectrumAnalyzer(SampleRate=wave.SampleRate,...
ViewType="spectrum-and-spectrogram",...
PlotAsTwoSidedSpectrum=false);
for a = 1:width(y)
scope2(y(:,a))
pause(0.01)
end
% read wav
[b,Fs] = audioread('test.wav');
info = audioinfo('test.wav');
% visualize output wav
scope3 = spectrumAnalyzer(SampleRate=wave.SampleRate,...
ViewType="spectrum-and-spectrogram",...
PlotAsTwoSidedSpectrum=false);
for a = 1:width(b)
scope3(b(:,a))
pause(0.01)
end
when I change BitsPerSample parameter to 32/64:
% write wav
audiowrite(wavefile,y,Fs,'BitsPerSample',64);
I get a right values/signal form.
Why it is happends? It is aliasing/quantization noise?

Accepted Answer

Mathieu NOE
Mathieu NOE on 8 Sep 2022
hello
if you export data to wav format , you have to make sure that the amplitude do not exceed +/- 1 otherwise the signal will be clipped and there you have a distorted signal (and that's why you get all the spurious frequencies)
seems to me this condition is not met as I can see the FFT peaks are well above 0 dB => clipping !
x = wave() + 0.5*wave2()+ 0.05*randn(1024,1);
% and normalize !!
x = x./max(abs(x));
  3 Comments
Nik Rocky
Nik Rocky on 13 Sep 2022
Hello Mathieu NOE,
thank you so much and sorry for late answer. Yes, its solves my problem!
If I inspect my x variable, thereare lot of values under -1:
-1.19660966653338
1.32569250089870
-1.29899512583688
1.30270363867150
-1.31475515734044
1.23504577190159
-1.35537389624305
1.21100708613462
-1.05105824664192
1.16503633855557
-0.848889696268412
...
and in the description of audiowrite funktion we can read:
Data beyond the valid range is clipped.
If y is single or double, then audio data in y should be normalized to values in the range −1.0 and 1.0, inclusive.
So, the normalizing solves this issue. Thank you very much!
Best regards,
Nik

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!