How do I add noise to a sine wave signal to get the output below

21 views (last 30 days)
Hi, I have been working this code for hours, I got stuck when I tried to the sine waves and add a noise to give the output below, here is the code I have so far
clear;
clc;
close;
% Frequency[Hz] = 1, Amplitude[w] = 10
f=1; %frequency [Hz]
% t = -0:.0001:pi;
t = 0:0.01:1;
a=3; %amplitude [w]
phi=0; %phase
subplot(4,1,1);
y=a*sin(2*pi*f*t+phi);
plot(t,y)
title('1Hz sine wave')
xlabel('time(s)')
ylabel('amplitude(w)')
% Frequency[Hz] = 5, Amplitude[w] = 2
f2=5; %frequency [Hz]
% t2=(0:1/(f2*100):1);
a2=2; %amplitude [w]
phi=0; %phase
subplot(4,1,2);
y2=a2*sin(2*pi*f2*t+phi);
plot(t,y2)
title('5Hz sine wave')
xlabel('time(s)')
ylabel('amplitude(w)')
% Frequency[Hz] = 1, Amplitude[w] = 10
f3=10; %frequency [Hz]
% t3=(0:1/(f3*100):1);
a3=1; %amplitude [w]
phi=0; %phase
subplot(4,1,3);
y3=a3*sin(2*pi*f3*t+phi);
plot(t,y3)
xlabel('time(s)')
title('10Hz sine wave')
ylabel('amplitude(w)')
subplot(4,1,4);
t = 0:0.1:10;
total_ampl = y+y2+y3;
noise = rand(size(t));
distorted = total_ampl + noise;
plot(t, distorted);
The desired output
Your assistance will be highly appreciated.

Accepted Answer

Mathieu NOE
Mathieu NOE on 13 Apr 2021
hello
simply the time vector was not correct , just define it once for all plots (and with max value = 10 s and not 1 s)
also the amount of noise shoudn't be too high , so I reduced its amplitude
clearvars;
clc;
close;
% Frequency[Hz] = 1, Amplitude[w] = 10
f=1; %frequency [Hz]
% t = -0:.0001:pi;
t = 0:0.01:10;
a=3; %amplitude [w]
phi=0; %phase
figure(1);
subplot(4,1,1);
y=a*sin(2*pi*f*t+phi);
plot(t,y)
title('1Hz sine wave')
xlabel('time(s)')
ylabel('amplitude(w)')
% Frequency[Hz] = 5, Amplitude[w] = 2
f2=5; %frequency [Hz]
a2=2; %amplitude [w]
phi=0; %phase
subplot(4,1,2);
y2=a2*sin(2*pi*f2*t+phi);
plot(t,y2)
title('5Hz sine wave')
xlabel('time(s)')
ylabel('amplitude(w)')
% Frequency[Hz] = 1, Amplitude[w] = 10
f3=10; %frequency [Hz]
a3=1; %amplitude [w]
phi=0; %phase
subplot(4,1,3);
y3=a3*sin(2*pi*f3*t+phi);
plot(t,y3)
xlabel('time(s)')
title('10Hz sine wave')
ylabel('amplitude(w)')
subplot(4,1,4);
total_ampl = y+y2+y3;
noise = 0.1*randn(size(t));
distorted = total_ampl + noise;
plot(t, distorted);
figure(2)
plot(t, distorted);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!