Matlab - problem with snr function
이전 댓글 표시
I have to add noise to recorded audio, so as SNR of result signal should be equal 3dB.
The idea is to calculate SNR of recorded audio and generated gaussian noise. According to that ratio I want to scale noise and then add it to recorded audio
I convert file to vector using audioread function:
[ y, fsampl] = audioread('123.wav');
And then I generate gaussian noise:
noise = normrnd(0,1,1,length(y));
Next I want to calculate signal to noise ratio using snr() function:
cur_snr= snr(y, noise);
But i get an error:
Error using snr
Expected input number 2, Fs, to be a scalar.
Error in snr>timeSNR (line 187)
validateattributes(fs,{'numeric'},{'real','finite','scalar','positive'}, ...
Error in snr (line 157)
[r, noisePow] = timeSNR(plotType, harmType, Args{:});
Error in untitled4 (line 10)
cur_snr= snr(y, noise);
I have no idea what can be wrong. What i checked:
-audio file is read corectly
-noise and y vector has the same length
채택된 답변
추가 답변 (1개)
Image Analyst
2021년 11월 12일
This works fine:
[y, fs] = audioread('guitartune.wav');
subplot(3, 1, 1);
plot(y, 'b-');
grid on;
title('Perfect Signal', 'FontSize', 20)
noiseAmplitude = 0.05 * max(y)
noise = noiseAmplitude * normrnd(0,1,1, length(y));
% Make a column vector like y
noise = noise(:);
subplot(3, 1, 2);
plot(noise, 'b-');
grid on;
title('Noise Alone', 'FontSize', 20)
% Create noisy signal
noise_y = y + noise;
subplot(3, 1, 3);
plot(noise_y, 'b-');
grid on;
cur_snr= snr(y, noise)
caption = sprintf('Noisy Signal. SNR = %.2f', cur_snr);
title(caption, 'FontSize', 20)

카테고리
도움말 센터 및 File Exchange에서 Acoustics, Noise and Vibration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!