Matlab - problem with snr function
Show older comments
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
Accepted Answer
More Answers (1)
Image Analyst
on 12 Nov 2021
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)

Categories
Find more on Acoustics, Noise and Vibration 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!