-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_5.asv
53 lines (45 loc) · 1.63 KB
/
experiment_5.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
s = 0.7*sin(2*pi*50*t) + sin(3*pi*100*t)+sin(5*pi*120*t)/5;
X = s %randn(size(t)); % Sinusoids plus noise
subplot(211)
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
ylabel('X(t)');
Y=fft(X);
p2= abs(Y/L); %here if L is removed we still get the results but to restrict the amplitude we have taken L
p1= p2(1:L/2+1);
p1(2:end-1)= 2*p1(2:end-1);
f= Fs*(0:(L/2))/L;
subplot(212)
plot(f,p1)
title('single-sided amplitude spectrum of X(t)')
xlabel('f(Hz)')
ylabel('p1(f)')
%%%%%%%%%%%%%%% WITH NOISE %%%%%%%%%%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
s = 0.7*sin(2*pi*50*t) + sin(3*pi*100*t)+sin(5*pi*120*t)/5;
X = s + randn(size(t)); % Sinusoids plus noise
subplot(211)
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
ylabel('X(t)');
Y=fft(X);
p2= abs(Y/L); %here if L is removed we still get the results but to restrict the amplitude we have taken L
p1= p2(1:L/2+1);
p1(2:end-1)= 2*p1(2:end-1);
f= Fs*(0:(L/2))/L;
subplot(222)
plot(f,p1)
title('single-sided amplitude spectrum of X(t)')
xlabel('f(Hz)')
ylabel('p1(f)')