-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS_Matlab_Project_2017.m
312 lines (252 loc) · 7.79 KB
/
JS_Matlab_Project_2017.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
close all;
clear all;
%% Part 1: Analysis and Reconstruction
file = 'Signal2.wav';
[x, Fs] = audioread(file); %x is an array containing the signal
Ts = 1 / Fs; %Ts is Sampling Period
Nsamps = length(x); %Nsamps is the length of array x
t = Ts * (1:Nsamps); %Prepare time data for plot
%Array is the length of Nsamps
%in increments of Ts
%% #2
%Plot Sound File in Time Domain
figure;
plot(t, x);
xlim([0 0.0115]); %3 Full Periods????
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Signal in Time Domain');
%% #3
%Do Fourier Transform
x_fft = abs(fft(x) /Fs); %Normalize Signal & Retain
%Magnitude
x_fft = x_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
%Plot Sound File in Frequency Domain
figure;
plot(f, x_fft);
xlim([0 1550]); %Show only 5 Harmonics
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency Spectrum of Original Signal');
%% #6
a0 = 0.0050; %DC COMPONENT
a1 = 0.0176;
a2 = 0.0256;
a3 = 0.0089;
a4 = 0.0034;
a5 = 0.004;
fund_freq = 262.5; %Fundamental Frequency ? (Obtained from Frequency Spectrum Graph) Hz
fund_period = (2 * pi) / fund_freq;
omega = 2 * pi * fund_freq; %rads/s
%6a
x1 = 2*a1*cos(omega * t);
figure
plot(t, x1);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('X1');
%6b
x2 = x1 + 2*a2*cos(2*omega*t);
figure
plot(t, x2);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('X2');
%6c
x3 = x2 + 2*a3*cos(3*omega*t);
figure
plot(t, x3);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('X3');
%6d
x4 = x3 + 2*a4*cos(4*omega*t);
figure
plot(t, x4);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('X4');
%6e
x5 = x4 + 2*a5*cos(5*omega*t);
figure
plot(t, x5);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('X5');
%% #7
%Do Fourier Transform
x5_fft = abs(fft(x5) /Fs); %Normalize Signal & Retain
%Magnitude
x5_fft = x5_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
%Plot x5 in Frequency Domain
figure;
plot(f, x5_fft);
xlim([0 1550]); %Show only 5 Harmonics
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency Spectrum of x5');
%% Part 2:Instrument filtered with 1st Order Low-Pass, cutoff frequency = 200 Hz
h1 = (1.2566e+3)*exp((-t)/(795.77e-6));
figure
plot(t, h1);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Impulse Response at 200 Hz (h1)');
%% #2
%Do Fourier Transform
h1_fft = abs(fft(h1) /Fs); %Normalize Signal & Retain
%Magnitude
h1_fft = h1_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
figure
plot(f, h1_fft);
xlim([0 1550]);
xlabel('Hz');
ylabel('Amplitude');
title('Frequency Spectrum of h1');
%% #3
%Convolution
output = conv(x, h1) * Ts;
Nsamps1 = length(output); %New Length Obtained from Convolution which extends signal (Different from Nsamps)
t1 = (Ts) * (1:Nsamps1);
figure
plot(t1, output);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Convolution x & h1 (output)');
%% #3b
%Do Fourier Transform
output_fft = abs(fft(output) /Fs); %Normalize Signal & Retain Magnitude
output_fft = output_fft(1:Nsamps1/2); %Discard half of points
f = Fs * (0:(Nsamps1/2)-1)/Nsamps1; %Prepare frequency data for plots
figure
plot(f, output_fft);
xlim([0 1550]);
xlabel('Hz');
ylabel('Amplitude');
title('Frequency Spectrum of output');
%% #3c
%Convert the output to a WAV file
output_file = 'Filtered_Audio.WAV';
audiowrite(output_file, output, Fs);
%% Part 3: Instrument filtered with 1st Order Low-Pass, cutoff freq = 1000Hz
h2 = (6.2832e+3)*exp((-t)/(159.1546e-6));
figure
plot(t, h2);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Impulse Response at 1000Hz (h2)');
%% #2
%Do Fourier Transform
h2_fft = abs(fft(h2) /Fs); %Normalize Signal & Retain
%Magnitude
h2_fft = h2_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
figure
plot(f, h2_fft);
xlim([0 1550]);
xlabel('Hz');
ylabel('Amplitude');
title('Frequency Spectrum of h2');
%% #3
%Convolution
output1 = conv(x, h2) * Ts;
Nsamps2 = length(output1); %New Length Obtained from convolution which extends signal (Different from Nsamps)
t = (Ts) * (1:Nsamps2);
figure
plot(t, output1);
xlim([0 0.01143]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Convolution x & h2 (output1)');
%% #3b
%Do Fourier Transform
y1_fft = abs(fft(output1) /Fs); %Normalize Signal & Retain
%Magnitude
y1_fft = y1_fft(1:Nsamps2/2); %Discard half of points
f = Fs * (0:(Nsamps2/2)-1)/Nsamps2; %Prepare frequency data for plots
figure
plot(f, y1_fft);
xlim([0 1550]);
xlabel('Hz');
ylabel('Amplitude');
title('Frequency Spectrum of output1');
%% #3c
%Convert the output to a WAV file
output_file1 = 'Filtered_Audio1.WAV';
audiowrite(output_file1, output1, Fs);
%% Part 4: Voice signal filter with 1st Order Low-Pass cutoff frequency = 100Hz
file = 'Voice.wav';
[x, Fs] = audioread(file); %x is an array containing the signal
Ts = 1 / Fs; %Ts is Sampling Period
Nsamps = length(x); %Nsamps is the length of array x
t = Ts * (1:Nsamps); %Prepare time data for plot
%Array is the length of Nsamps
%in increments of Ts
%Plot Voice File in Time Domain
figure;
plot(t, x);
xlim([0.3328 4]) %only plot from o to 0.06s
xlabel('Time (s)');
ylabel('Amplitude');
title('Voice Signal in Time Domain');
%Do Fourier Transform
x_fft = abs(fft(x) /Fs); %Normalize Signal & Retain
%Magnitude
x_fft = x_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
%Plot Voice File in Frequency Domain
figure;
plot(f, x_fft);
xlim([0 1500]);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency Spectrum of Voice Signal');
h3 = (0.63e+3)*exp(-t*(0.63e+3));
%Do Fourier Transform
h3_fft = abs(fft(h3) /Fs); %Normalize Signal & Retain
%Magnitude
h3_fft = h3_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
%Plot h3 in Frequency Domain
figure;
plot(f, h3_fft);
xlim([0 1500]);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency Spectrum of h3');
%Convolution
output3 = conv(x, h3) * Ts;
Nsamps = length(output3);
t = (Ts) * (1:Nsamps);
figure
plot(t, output3);
xlim([0.3328 4]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Convolution Impulse Response & Voice Signal (output3)');
%Do Fourier Transform
output3_fft = abs(fft(output3) /Fs); %Normalize Signal & Retain
%Magnitude
output3_fft = output3_fft(1:Nsamps/2); %Discard half of points
f = Fs * (0:(Nsamps/2)-1)/Nsamps; %Prepare frequency data for plots
%Plot output3 in Frequency Domain
figure;
plot(f, output3_fft);
xlim([0 1500]);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Frequency Spectrum of Output3');
%Convert the output to a WAV file
output_file = 'Filtered_Voice.wav';
audiowrite(output_file, output3, Fs);