How to insert noise in a signal using matlab?

Hi. I want to create a signal with noise. I found this coding but I don't know how it works. Can somebody explain this to me?
x= cos(2*pi*12*[0:0.001:1.23]);
x(end) = [];
[b a] = butter(2,[0.6 0.7],'bandpass');
filtered_noise = filter(b,a,randn(1, length(x)*2));
x = (x + 0.5*filtered_noise(500:500+length(x)-1))/length(x)*2;
This is the result when I plot the x.

 Accepted Answer

It creates a signal in ‘x’, then creates band-limited noise by bandpass-filtering a Gaussian random vector (created by the randn call), the same length as ‘x’, and stores it in the ‘filtered_noise’ variable. It then adds that to the original ‘x’ and scales it to create the final value of ‘x’. (There is a bit more involved in the code than I described, but that is the essence of it.)

6 Comments

Thank you for your answer. If I'm not mistaken, the general equation for sinusoidal signal is 2*pi*f*t, so the frequency of the signal is 12 Hz. Am I right? And from the code, can I get the information about the noise? What is the frequency of the noise?
My pleasure.
If the time is in seconds, that would be correct (as I read it).
The sampling interval is Ts=0.001 (again I assume seconds), so the sampling frequency, Fs=1000 Hz, and the Nyquist frequency, Fn=500 Hz. Since the passband frequencies for the filter are normalised to Fn, the frequency bandwidth of the noise would be [0.6 0.7]*Fn, or [300 350] Hz.
Again, thank you so much for your answer. I can understand it very well. How about the final amplitude? From the result, it is almost 2x10-3. I also do not understand the x-axis parameter, which is samples. How did it turn out that way?
My pleasure.
That is correct, since the amplitude is normalised by the half the length of ‘x’.
The x-axis is defined in the command that plotted the figure, so you would have to see the code. The samping interval is 1 ms, so if you want the value of the x-axis in seconds (again assuming those are the correct units), multiply the values by 0.001.
The length of 'x' is 1230, so the final amplitude is 1/(1230/2) = 1.63x10-3. Okay I understand it now. Thanks! Now if you don't mind, can you help me with the next step? I want to plot the frequency spectrum of signal 'x'.
%plot magnitude spectrum of the signal
X_mags = abs(fft(x));
figure,plot(X_mags);
xlabel('DFT Bins');
ylabel('Magnitude');
Then, this code is to plot the first half of DFT. I don't quite understand this part. Can you explain this to me?
%plot first half of DFT (normalised frequency)
num_bins = length(X_mags);
figure,plot([0:1/(num_bins/2 -1):1], X_mags(1:num_bins/2));
xlabel('Normalised frequency (\pi rads/sample)');
ylabel('Magnitude');
That appears to be correct.
If you want the frequency in Hz (again assuming the time unit is seconds), multiply the normalised frequency by Fn, or 500 Hz.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!