how to insert noise in a sine function.
246 views (last 30 days)
Shashank Prasanna on 11 Sep 2013
I'd use randn because white noise or gaussian noise is more natural then uniform random noise.
noisy_y = y + noiseAmplitude * randn(1, length(y));
You don't need this but if you do have the Communication Systems Tbx, there are function that do this automatically for you. Example:
Jan on 12 Sep 2013
Edited: Jan on 12 Sep 2013
rand or randn might be sufficient. But 1:length(y) is true noise also and can be 100% natural.
There is no unique definition of noise and therefore asking for "adding noise" is not enough information to find a meaningful answer. Please explain what kind of noise you need and e.g. the purpose of the noise. You can add noise to the Y-values, but to the X-values also, or to both.
Notice that even y = sin(-pi:.1:pi) contains noise already, because due to the limited precision (tiny effect) of double 's and the low sampling rate (huge effect) the resulting vector does not sound like a clean sinus signal when you feed wavplay with it.
W. Owen Brimijoin on 13 Sep 2013
Provided what you are looking for is white noise, then Image Analyst's solution:
noisy_y = y + noiseAmplitude * rand(1, length(y));
...will get you most of the way there, with one caveat. This method will add noise with only a positive sign, thus introducing a DC offset to your signal that's on average noiseAmpitude/2.
I might suggest a simple addition to this line that removes this offset (and I've also reoriented the noise array dimension so that it adds properly):
noisy_y = y + noiseAmplitude * 2*(rand(length(y),1)-.5);
If you are looking for other forms of noise (pink or otherwise), you might try peeking at:
on the FileExchange