-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deniz_Karabacak_FINAL.m
194 lines (132 loc) · 3.08 KB
/
Deniz_Karabacak_FINAL.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
close all;
clc;
clearvars;
%%
Fs = 4000;
Fc = 700;%%low pass filter
n=10; %% filter order
N = 1024 ; %%fft length
f = 250; %%standart freq
t = 0:0.01:0.1; %% time interval
m = 2*sin(2*pi*f*t) + 1.5*cos(2*pi*5*f*t) + cos(2*pi*(f/2)*t);
c1 = exp(j*2*pi*400*t);
c2 = exp(-1*j*2*pi*400*t);
x = m.*c1;
[b_low,a_low] = butter(n,Fc/(Fs/2),'low'); %low pass filter
h_low = freqz(b_low,a_low,Fs/2);%magnitude
z = filter(b_low,a_low,x);
y = c2.*z;
v=0;
sympref('HeavisideAtOrigin',v);
h = heaviside(t-0.05) - heaviside(t-0.08);
w = conv(y,h);
conv_time = length(y) + length(h) -1;
m_fourier= fftshift(abs(fft(m,N)))/N;
x_fourier= fftshift(abs(fft(x,N)))/N;
z_fourier= fftshift(abs(fft(z,N)))/N;
y_fourier= fftshift(abs(fft(y,N)))/N;
forier_time = linspace(-Fs/2,Fs/2,N);
figure;
subplot(4,1,1);
plot(forier_time,m_fourier);
grid on
title("Magnitude of Frequency Response of M")
ylabel("Magnitude");
xlabel("Frequency");
subplot(4,1,2);
plot(forier_time,x_fourier);
grid on
title("Magnitude of Frequency Response of X")
ylabel("Magnitude");
xlabel("Frequency");
subplot(4,1,3);
plot(forier_time,z_fourier);
grid on
title("Magnitude of Frequency Response of Z")
ylabel("Magnitude");
xlabel("Frequency");
subplot(4,1,4);
plot(forier_time,y_fourier);
grid on
title("Magnitude of Frequency Response of Y")
ylabel("Magnitude");
xlabel("Frequency");
figure;
subplot(3,1,1);
plot(t,y);
grid on;
title("y(t)");
ylabel("x");
xlabel("time");
subplot(3,1,2);
plot(t,h);
grid on;
title("h(t)");
ylabel("x");
xlabel("time");
subplot(3,1,3);
plot(conv_time,w);
grid on;
title("w(t)");
ylabel("x");
xlabel("time");
%%
close all;
clc;
clearvars;
%%
%question 2
Fs=2000;
t = -0.05:(1/Fs):0.05;
f1 = 100;
f2 = 300;
f3 = 500;
N = 1024;
n = 25;
fcut1 = 250;
fcut2 = 350;
fcut3 = [fcut1,fcut2];
x = cos(2*pi*f1*t) + cos(2*pi*f2*t) + cos(2*pi*f3*t);
X = fftshift(abs(fft(x,N))) /N;
fft_time = linspace(-Fs/2,Fs/2,N);
figure;
subplot(2,1,1);
plot(t,x)
grid on
title("x(t)")
ylabel("x");
xlabel("time");
subplot(2,1,2);
plot(fft_time,X);
grid on
title("Frequency response of Magnitude of x")
ylabel("X(jw)");
xlabel("frequency");
[b_high1,a_high1] = butter(n,fcut1/(Fs/2),'high'); %high pass filter
h_high1 = freqz(b_high1,a_high1,Fs/2);%magnitude
y1 = filter(b_high1,a_high1,x);
[b_low1,a_low1] = butter(n,fcut2/(Fs/2),'low'); %low pass filter
h_low1 = freqz(b_low1,a_low1,Fs/2);%magnitude
y2 = filter(b_low1,a_low1,y1);
[b_bandpass_1,a_bandpass_1] = butter(n,fcut3/(Fs/2),'bandpass'); %bandpass filter
h_bandpass_1 = freqz(b_bandpass_1,a_bandpass_1,Fs/2);%magnitude
y3 = filter(b_bandpass_1,a_bandpass_1,x);
figure;
subplot(3,1,1);
plot(t,y1);
grid on
title('High Pass Filter');
xlabel("time");
ylabel("filtered x");
subplot(3,1,2);
plot(t,y2);
grid on
title('High & low pass Filter');
xlabel("time");
ylabel("filtered x");
subplot(3,1,3);
plot(t,y3);
grid on
title('Band pass Filter');
xlabel("time");
ylabel("filtered x");