-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.m
49 lines (43 loc) · 964 Bytes
/
square.m
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
%% square
clc; clear all; close all;
t=0:0.1:20;
L=300;
Fs=1000;
y=sin(t)+(sin(3*t)/3)+(sin(5*t)/5)+(sin(7*t)/7)+(sin(9*t)/9);
subplot(211)
plot(t(1:70),y(1:70))
title('square Signal with no Random Noise')
xlabel('time (milliseconds)')
ylabel('X(t)')
Y=fft(y);
P2=abs(Y/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 y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%% with noise
figure
t=0:0.1:20;
L=300;
Fs=1000;
y=sin(t)+(sin(3*t)/3)+(sin(5*t)/5)+(sin(7*t)/7)+(sin(9*t)/9);
x = y + 2*randn(size(t));
subplot(211)
plot(t(1:70),x(1:70))
title('square Signal with Random Noise')
xlabel('time (milliseconds)')
ylabel('X(t)')
Y=fft(x);
P2=abs(Y/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 y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')