-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIIRFilters.asv
114 lines (93 loc) · 3.25 KB
/
IIRFilters.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
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
%% Setup
clc, clear, close all;
% Player center frequencies in H
f_players = [1471, 1724, 2000, 2273, 2632, 2941, 3333, 3571, 3846, 4167];
denoms = zeros(11,10);
nums = zeros(11,10);
f_s = 10000; % sampling frequency (remember we have decimated)
BW = 50; % Full bandwidth of filters in Hz
%% 3.1
figure;
hold on;
for i = 1:10
f_lower = f_players(i) - BW/2; % Lower corner frequency of BPF
f_upper = f_players(i) + BW/2; % Upper corner frequency of BPF
[b1, a1] = butter(5, [f_lower*2/f_s, f_upper*2/f_s]);
nums(:, i) = b1;
denoms(:, i) = a1;
% IMPORTANT: ‘butter’ takes frequency specifications in half cycles/sample,
% not Hz, hence the division by f_s and multiplication by 2
f_axis = linspace(1000, 4500, 2000); % create a freq axis from 1 to 4.5 kHz
H1 = freqz(b1, a1, f_axis, f_s); % calculates the freq response H1
% at the frequencies in f axis
%plot(f_axis, abs(H1)); % magnitude of the frequency response
H_db = 20 * log10(abs(H1));
plot(f_axis, H_db);
end
%% SAVE NUMERATORS AND DENOMINATORS
a_iir = transpose(denoms);
b_iir = transpose(nums);
%% save the files
save a1.txt a_iir -ascii -double;
save b1.txt b_iir -ascii -double;
%% Graph
hold off;
xlabel("Frequency (Hz)");
ylabel("Transmission");
title("Frequency Respsonses");
legend("1471 Hz", "1724 Hz", "2000 Hz", "2273 Hz", "2632 Hz", "2941 Hz", "3333 Hz", "3571 Hz", "3846 Hz", "4167 Hz")
%% Square wave
Fs = 10e3; % The sampling frequency
t = linspace(0, 0.2, 2000); % The time vector with a length of 0.2 seconds and 2000 total samples
freq = 1471; % The frequency of player 1
data_vect(:,1) = square(2*pi*f_players(1)*t); % Create the time domain square wave
data_vect(:,2) = square(2*pi*f_players(2)*t);
data_vect(:,3) = square(2*pi*f_players(3)*t);
data_vect(:,4) = square(2*pi*f_players(4)*t);
data_vect(:,5) = square(2*pi*f_players(5)*t);
data_vect(:,6) = square(2*pi*f_players(6)*t);
data_vect(:,7) = square(2*pi*f_players(7)*t);
data_vect(:,8) = square(2*pi*f_players(8)*t);
data_vect(:,9) = square(2*pi*f_players(9)*t);
data_vect(:,10) = square(2*pi*f_players(10)*t);
%% Filter the square waves
data_vect = zeros(2000,10);
energy_vals = zeros(10,10);
%data_vect(:,1) = filter(b_iir(1,:), a_iir(1,:), x1);
%data_vect(:,2) = filter(b_iir(2,:), a_iir(2,:), x2);
%data_vect(:,3) = filter(b_iir(3,:), a_iir(3,:), x3);
%data_vect(:,4) = filter(b_iir(4,:), a_iir(4,:), x4);
%data_vect(:,5) = filter(b_iir(5,:), a_iir(5,:), x5);
%data_vect(:,6) = filter(b_iir(6,:), a_iir(6,:), x6);
%data_vect(:,7) = filter(b_iir(7,:), a_iir(7,:), x7);
%data_vect(:,8) = filter(b_iir(8,:), a_iir(8,:), x8);
%data_vect(:,9) = filter(b_iir(9,:), a_iir(9,:), x9);
%data_vect(:,10) = filter(b_iir(10,:), a_iir(10,:), x10);
%% Calculate Energies through Filters
for i = 1:10
sample = data_vect(:,i);
for j = 1:10
num = num
energy_vals(i, j) = sum(abs(sample).*abs(sample));
end
end
%% Test plot
figure;
bar(energy_vals(1,:));
%% plotting
figure;
for i = 1:10
subplot(10,1,i);
bar(energy_vals(i,:));
xlabel("Filter Number");
ylabel("Energy Of Signal");
title("Signal" + i);
end
%%
figure;
subplot(2,1,1);
bar(Y1(1,:));
xlabel("Filter Number");
ylabel("Energy Of Signal");
title("Energy of Filtered Signals(Easy - 1)");
%% Energy