-
Notifications
You must be signed in to change notification settings - Fork 0
/
Untitled2.m
186 lines (151 loc) · 4.79 KB
/
Untitled2.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
%%
close all;
clear all;
clc
indx=[1 6 0 1 9 8] % input your index no as a matrix
% getting A B C
A=indx(4);
B=indx(5);
C=indx(6);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Parameters%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%parameters for the BPF
A_p=0.05+0.01*A;
A_a=40+B;
Wp1=C*100+400;
Wp2=C*100+700;
Wa1=C*100+250;
Wa2=C*100+800;
Ws=2*(C*100+1200);
fs=Ws/(2*pi);%sampling freq
L=2048;% no of samples for plot input signals
%%%%%%%%%%%%%%%%%%%%%%%%%%Kaiser Window Generation%%%%%%%%%%%%%%%%%%%%%%%
%%
% get critical transition width
Bt=min((Wp1-Wa1),(Wa2-Wp2));
Wc1=Wp1-Bt/2;%ideal lower cutoff freq
Wc2= Wp2+Bt/2;%ideal upper cutoff freq
%get actual passband ripple
delt_P=(10^(0.05*A_p)-1)/(10^(0.05*A_p)+1);
delt_A=10^(-0.05*A_a);
delta=min(delt_P,delt_A);
%actual stopband attenuation & passband attenuation calculation
Aa= - 20*log10(delta);
%get parameter alpha
if Aa<=21
alpha=0;
elseif Aa<=50
alpha=0.5842*(Aa-21)^0.4+0.07886*(Aa-21);
else
alpha=0.1102*(Aa-8.7);
end
%get parameter D
if Aa<=21
D=0.9222;
else
D=(Aa-7.95)/14.36;
end
%select N- Order of the filter
temp_var=(Ws*D/Bt)+1;
if (temp_var-fix(temp_var)) ==0
if mod(temp_var,2)==1
N=temp_var;
else
N=temp_var+1;
end
else
if mod(fix(temp_var),2)==0
N=fix(temp_var)+1;
else
N=fix(temp_var)+2;
end
end
%get M
M=(N-1)/2;
%set non zero domain for window
n=linspace(-M,M,2*M+1);
%get the beta values
beta=alpha*sqrt(1-(n/M).^2);
num=getBessel(beta);
denom=getBessel(alpha);
%kiaser window generation
w_n=num/denom;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%ideal BPF generation
h_n=sin(Wc2*n/fs)./(pi*n)-sin(Wc1*n/fs)./(pi*n);
h_n(M+1)=(Wc2-Wc1)*2/Ws;
%resulting FIR BP Filter
hw_n=h_n.*w_n;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Analysis%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
[h,f]=freqz(hw_n);%frequency response of filter in normalized freq range
f=f*(Ws/(2*pi));%freq domain correction
H_db=20*log10(abs(h));%get the magnitude in db scale
%causal impulse response of the filter
figure;
stem(n(M+1:2*M+1),hw_n(M+1:2*M+1));
xlabel('n (Samples)');ylabel('Amplitude');title('impulse response of the filter')
minPassRip=-A_p/2*ones(1,length(f));
maxStopRip=-A_a*ones(1,length(f));
maxPassRip=A_p/2*ones(1,length(f));
%frequency response of the filter
figure;
plot(f,H_db);
hold on;
plot(f,minPassRip);
hold on;
plot(f,maxPassRip);
hold on;
plot(f,maxStopRip);
xlabel('Frequency (rad/s)');ylabel('Magnitude (dB)');title('Frequency response of the bandpass FIR filter (Kaiser Windowed)')
figure;
plot(f,abs(h));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('BP filter');
%%%%%%%%%%%%%%%%%%%%%%validating results%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
n=(0:L-1);%integer domain
f=(-L/2:L/2-1)*(Ws/L);%frequency domain
%input signal
x_n=sin(n*(Wc1+Wc2)/(2*fs))+sin(n*(0+Wa1)/(2*fs))+sin(n*(Wa2+Ws/2)/(2*fs));
t_n=sin(n*(Wc1+Wc2)/(2*fs));%ideal output
%%%%%%%%%%%%%%%%%%%%%%%frequency domain %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%get DFT of signals
X_n=fftshift(fft(x_n,L));
H_n=fftshift(fft(hw_n,L));
T_n=fftshift(fft(t_n,L));
%convolve two signals in time domain (freq domain multiplication)
Y_n=(X_n.*H_n);
%plot results
figure;
subplot(3,1,1);plot(f,abs(X_n/L));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('Input signal');
subplot(3,1,2);plot(f,abs(H_n));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('BP filter');
subplot(3,1,3);plot(f,abs(Y_n/L));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('Output signal');
figure;
subplot(2,1,1);plot(f,abs(Y_n/L));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('Output of the FIR BPF in Frequency Domain');
subplot(2,1,2);plot(f,abs(T_n/L));
xlabel('Frequency (rad/s)');ylabel('Magnitude');title('Output of the Ideal BPF in Frequency Domain');
%%%%%%%%%%%%%%%%%%%%%%%%%get time domain results%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%arrange the filter
hw_n1=zeros(1,L);
hw_n1(floor(L/2)-M:floor(L/2)+M)=hw_n;
%filter the signal
y_n=conv(x_n,hw_n,'full');
%plot results
figure;
subplot(3,1,1);plot(n,x_n);
ylim([min(x_n) max(x_n)]);xlim([0 L]);xlabel('Samples (n)');ylabel('Amplitude');title('Input signal');
subplot(3,1,2);plot(n,hw_n1);
ylim([min(hw_n1) max(hw_n1)]);xlim([0 L]);xlabel('Samples (n)');ylabel('Amplitude');title('FIR BPF in time domain ');
subplot(3,1,3);plot(n,y_n(M:L+M-1));
ylim([-1 1]);xlim([0 L]);xlabel('Samples (n)');ylabel('Amplitude');title('Output of the FIR BPF');
figure;
subplot(2,1,1);plot(n,y_n(M:L+M-1));
xlabel('Samples (n)');ylabel('Amplitude');title('Output of the FIR BPF');ylim([-1 1]);xlim([0 L]);
subplot(2,1,2);
plot(n,t_n);
ylim([-1 1]);xlim([0 L]);xlabel('Samples (n)');ylabel('Amplitude');title('Output of the ideal BPF');