I'm trying to add White Gaussian Noise to an audio file. However; the energy of the noise should be 1/10th of that of the signal. My first attempt is as following:

[y,Fs] = audioread('drum.wav');
noisy_sig = addnoise(y,10);

function noisy_sig = addnoise( sig , SNRdb )
   sig_power = norm(sig,2) / length(sig);
   % noise power is equal to sigma^2
   sigma2 = sig_power / 10^(SNRdb/10) ;
   noisy_sig = sig + sqrt(sigma2)*randn(size(sig));

When I listen the resulting wav, there is no difference between the original and noisy ones. Am I doing something wrong? Any help would be appreciated.

  • $\begingroup$ So, Is it correct to say x = y + 0.1*randn(length(y),1) ? The energy of the noise signal should be 1/10th of the original signal. $\endgroup$ – Jason Dec 18 '19 at 15:31

Your problem is how you calculate the signal power. You are calling norm which calculates $\sqrt{\sum_i |x_i|^2}$ but you want to calculate the sum of the squared values and then divide by the length as you do in your code. Try this instead: sig_power = norm(sig)^2 / length(sig);. This line will calculate $\frac{1}{N}\sum_{i=1}^N |x_i|^2$.

  • $\begingroup$ Sir, without using this function; can I add the noise like that: x = y + sqrt(0.1) *randn(length(y),1) ? Is this a correct way of adding a noise whose energy is 1/10 of the original signal? $\endgroup$ – Jason Dec 18 '19 at 17:03
  • $\begingroup$ That adds noise with variance $0.1$. But from your comment you don't mention anything about y's power. SNR is signal-to-noise ratio so it is the signal and noise power relative to each other. $\endgroup$ – Engineer Dec 18 '19 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.