how to insert noise in a sine function.

172 views (last 30 days)
sanky kumar
sanky kumar on 11 Sep 2013
eg: x = -pi:.1:pi; y = sin(x); plot(x,y) i want to add noise to this signal. help appreciated!

Answers (4)

Image Analyst
Image Analyst on 11 Sep 2013
noisy_y = y + noiseAmplitude * rand(1, length(y));

Shashank Prasanna
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:
Shashank Prasanna
Shashank Prasanna on 16 Sep 2013
Edited: Shashank Prasanna on 16 Sep 2013
You are right IA, My answer seems to suggest white and gaussian noise are the same, while infact gaussian is one form of white noise. In most engineering applications however they are used interchangeably (albeit as you point out, erroneously). This assumption is almost fundamental and holds well due to central limit theorem. Also, any further analysis (like say regression) assume normally distributed noise.

Sign in to comment.

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
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

Community Treasure Hunt

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

Start Hunting!