-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.m
366 lines (288 loc) · 8.81 KB
/
app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
clc
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To load filters in workspace %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cd 'project location' %
% load('filters.mat') %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('filters.mat');
%get wave file
[file,path] = uigetfile('*.wav');
info = audioinfo(file);
[x,Fs] = audioread(file);
%gain for each band
gain = [];
g = inputdlg({'Enter the gain for 0-170 Hz band (in db)','Enter the gain for 170-310 Hz band (in db)','Enter the gain for 310-600 Hz band (in db)','Enter the gain for 600-1000 Hz band (in db)','Enter the gain for 1-3 KHz band (in db)','Enter the gain for 3-6 KHz band (in db)','Enter the gain for 6-12 KHz band (in db)','Enter the gain for 12-14 KHz band (in db)','Enter the gain for 14-16 KHz band (in db)'},...
'Amplification and Attenuation',[1 55]);
for i = 1:9
gain = [gain str2double(g{i})];
end
%type of filter
index = listdlg('PromptString',{'Choose the type of filters'},...
'SelectionMode','single','ListString',{'IIR','FIR'});
if index == 1
type = 'IIR';
else type = 'FIR';
end
Os = str2double(inputdlg({'Enter output sampling rate (in Hz)'})); %output sample rate
% Analysis function
% analyze('IIR');
if type == 'IIR'
y1=filter(iir170,x);
analyseFilter(y1,x,info,'0-170 Hz IIR Filter');
y1 = y1 .* db2mag(gain(1));
y2=filter(iir170310,x);
analyseFilter(y2,x,info,'170-310 Hz IIR Filter');
y2 = y2 .* db2mag(gain(2));
y3=filter(iir310600,x);
analyseFilter(y3,x,info,'310-600 Hz IIR Filter');
y3 = y3 .* db2mag(gain(3));
y4=filter(iir6001000,x);
analyseFilter(y4,x,info,'600-1000 Hz IIR Filter');
y4 = y4 .* db2mag(gain(4));
y5=filter(iir13k,x);
analyseFilter(y5,x,info,'1-3 KHz IIR Filter');
y5 = y5 .* db2mag(gain(5));
y6=filter(iir36k,x);
analyseFilter(y6,x,info,'3-6 KHz IIR Filter');
y6 = y6 .* db2mag(gain(6));
y7=filter(iir612k,x);
analyseFilter(y7,x,info,'6-12 KHz IIR Filter');
y7 = y7 .* db2mag(gain(7));
y8=filter(iir1214k,x);
analyseFilter(y8,x,info,'12-14 KHz IIR Filter');
y8 = y8 .* db2mag(gain(8));
y9=filter(iir1416k,x);
analyseFilter(y9,x,info,'14-16 KHz IIR Filter');
y9 = y9 .* db2mag(gain(9));
else
y1=filter(fir170,x);
analyseFilter(y1,x,info,'0-170 Hz FIR Filter');
y1 = y1 .* db2mag(gain(1));
y2=filter(fir170310,x);
analyseFilter(y2,x,info,'170-310 Hz FIR Filter');
y2 = y2 .* db2mag(gain(2));
y3=filter(fir310600,x);
analyseFilter(y3,x,info,'310-600 Hz FIR Filter');
y3 = y3 .* db2mag(gain(3));
y4=filter(fir6001000,x);
analyseFilter(y4,x,info,'600-1000 Hz FIR Filter');
y4 = y4 .* db2mag(gain(4));
y5=filter(fir13k,x);
analyseFilter(y5,x,info,'1-3 KHz FIR Filter');
y5 = y5 .* db2mag(gain(5));
y6=filter(fir36k,x);
analyseFilter(y6,x,info,'3-6 KHz FIR Filter');
y6 = y6 .* db2mag(gain(6));
y7=filter(fir612k,x);
analyseFilter(y7,x,info,'6-12 KHz FIR Filter');
y7 = y7 .* db2mag(gain(7));
y8=filter(fir1214k,x);
analyseFilter(y8,x,info,'12-14 KHz FIR Filter');
y8 = y8 .* db2mag(gain(8));
y9=filter(fir1416k,x);
analyseFilter(y9,x,info,'14-16 KHz FIR Filter');
y9 = y9 .* db2mag(gain(9));
end
y = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9;
% output of composite
idx = 1:info.TotalSamples;
t = (idx-1)./Os;
Fvec = linspace(-Os/2,Os/2,length(t));
Y = fftshift(fft(y));
figure('name','Composite Signal');
subplot(3,2,[1 2]);
plot(t,x);grid;
title('Original Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,[3 4]);
plot(t,y);grid;
title('Composite Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,5);
plot(Fvec,abs(Y));grid;
title('Magnitude spectrum');
xlabel('Frequency (Hz)');
ylabel('|Y(\omega)|');
xlim([-3,3]);
subplot(3,2,6);
plot(Fvec,angle(Y).*180/pi);grid;
title('Phase spectrum');
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
xlim([-3,3]);
% sound(y,Os);
audiowrite('composite.wav',x,Os);
% Doubling Fs
fs = Os*2;
idx = 1:info.TotalSamples;
t = (idx-1)./fs;
Fvec = linspace(-fs/2,fs/2,length(t));
Y = fftshift(fft(y));
figure('name','Double Sampling');
subplot(3,2,[1 2]);
plot(t,x);grid;
title('Original Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,[3 4]);
plot(t,y);grid;
title('Composite Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,5);
plot(Fvec,abs(Y));grid;
title('Magnitude spectrum');
xlabel('Frequency (Hz)');
ylabel('|Y(\omega)|');
xlim([-3,3]);
subplot(3,2,6);
plot(Fvec,angle(Y).*180/pi);grid;
title('Phase spectrum');
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
xlim([-3,3]);
% Decreasing Fs to half
fs = Os/2;
idx = 1:info.TotalSamples;
t = (idx-1)./fs;
Fvec = linspace(-fs/2,fs/2,length(t));
Y = fftshift(fft(y));
figure('name','Half sampling');
subplot(3,2,[1 2]);
plot(t,x);grid;
title('Original Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,[3 4]);
plot(t,y);grid;
title('Composite Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,5);
plot(Fvec,abs(Y));grid;
title('Magnitude spectrum');
xlabel('Frequency (Hz)');
ylabel('|Y(\omega)|');
xlim([-3,3]);
subplot(3,2,6);
plot(Fvec,angle(Y).*180/pi);grid;
title('Phase spectrum');
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
xlim([-3,3]);
% filtered signals analysis
function analyseFilter(y,x,info,name)
Fs = info.SampleRate;
idx = 1:info.TotalSamples;
t = (idx-1)./Fs;
Fvec = linspace(-Fs/2,Fs/2,length(t));
Y = fftshift(fft(y));
figure('name',name);
subplot(3,2,[1 2]);
plot(t,x);grid;
title('Original Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('x(t)');
subplot(3,2,3);
plot(t,y);grid;
title('Filtered Signal (Time domain)');
xlabel('Time (seconds)');
ylabel('y(t)');
subplot(3,2,4);
plot(Fvec,Y);grid;
title('Filtered Signal (Frequency domain)');
xlabel('Frequency (Hz)');
ylabel('Y(\omega)');
subplot(3,2,5);
plot(Fvec,abs(Y));grid;
title('Magnitude spectrum');
xlabel('Frequency (Hz)');
ylabel('|Y(\omega)|');
xlim([-3,3]);
subplot(3,2,6);
plot(Fvec,angle(Y).*180/pi);grid;
title('Phase spectrum');
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
xlim([-3,3]);
end
% filters' analysis function
function analyze(type)
load('filters.mat');
if type == 'IIR'
% IIR Analysis
% 0-170 Hz band
analysis(iir170,'4-th order 0-170 Hz BPF');
% 170-310 Hz band
analysis(iir170310,'4-th order 170-310 Hz BPF');
% 310-600 Hz band
analysis(iir310600,'4-th order 310-600 Hz BPF');
% 600-1000 Hz band
analysis(iir6001000,'4-th order 600-1000 Hz BPF');
% 1-3 KHz band
analysis(iir13k,'4-th order 1-3 KHz BPF');
% 3-6 KHz band
analysis(iir36k,'4-th order 3-6 KHz BPF');
% 6-12 KHz band
analysis(iir612k,'4-th order 6-12 KHz BPF');
% 12-14 KHz band
analysis(iir1214k,'4-th order 12-14 KHz BPF');
% 14-16 KHz band
analysis(iir1416k,'4-th order 14-16 KHz BPF');
else
% FIR Analysis
% 0-170 Hz band
analysis(fir170,'800-th order 0-170 Hz BPF');
% 170-310 Hz band
analysis(fir170310,'800-th order 170-310 Hz BPF');
% 310-600 Hz band
analysis(fir310600,'800-th order 310-600 Hz BPF');
% 600-1000 Hz band
analysis(fir6001000,'800-th order 600-1000 Hz BPF');
% 1-3 KHz band
analysis(fir13k,'800-th order 1-3 KHz BPF');
% 3-6 KHz band
analysis(fir36k,'800-th order 3-6 KHz BPF');
% 6-12 KHz band
analysis(fir612k,'800-th order 6-12 KHz BPF');
% 12-14 KHz band
analysis(fir1214k,'800-th order 12-14 KHz BPF');
% 14-16 KHz band
analysis(fir1416k,'800-th order 14-16 KHz BPF');
end
end
function analysis(filter,name)
figure('name',name);
[H,wh] = freqz(filter);
subplot(4,2,1);
plot(wh/pi,abs(H));grid;
title('Magnitude response');
xlabel('Normalized frequency (\times\pi rad/samples)');
ylabel('Magnitude');
[P,wp] = phasez(filter);
subplot(4,2,2);
plot(wp/pi,P.*180/pi);grid;
title('Phase response');
xlabel('Normalized frequency (\times\pi rad/samples)');
ylabel('Phase (Degrees)');
[h,nh] = impz(filter);
subplot(4,2,[3 4]);
stem(nh,h);grid;
title('Impulse response');
xlabel('Samples');
ylabel('Amplitude');
[s,ns] = stepz(filter);
subplot(4,2,[5 6]);
stem(ns,s);grid;
title('Step response');
xlabel('Samples');
ylabel('Amplitude');
subplot(4,2,[7 8]);
[b,a] = tf(filter);
zplane(b,a);grid;
title('Pole-Zero plot');
end