-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZeroDelay.m
292 lines (215 loc) · 10 KB
/
ZeroDelay.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
%x = audioread('Guitar_Input.wav");
%h = audioread('ir.wav');
%Fill input signal vector with 1's
x = ones(1024);
%Fill impulse response vector with 1's
h = ones(1024);
%starts incrementing from 0 onwards; For example, if there were 16 blocks,
%counter would go from 0 to 15
block_Counter = 0;
%assuming that the number of blocks goes from h0 to h6.
%the size of each block is assumed to be 64 samples
%this would yield an IR of exactly 1024 samples
%the number of samples in the input signal is assumed to be a power of 2;
%for simplicity sake, let us assume that the input signal is also 1024
%samples
N = 64;
%convolve the first N samples with first half of h0
output = conv(x(block_Counter*N + 1 : N+block_Counter*N),h(1:N));
%play output
soundsc(output,96000);
%fast convolve h1 with first N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_1_1 = IR.*X;
OUT_1_1 = ifft(output_1_1(1:0.5*length(output_1_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_1_1 = output_1_1(0.5*length(output_1_1)+1:length(output_1_1));
%fast convolve h2 with first N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_1_2 = IR.*X;
OUT_1_2 = ifft(output_1_2(1:0.5*length(output_1_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_1_2 = output_1_2(0.5*length(output_1_2)+1:length(output_1_2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 1 to 2N samples with h0
output = conv(x( (block_Counter - 1)*N + 1 : N+block_Counter*N), h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with second N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_2_1 = IR.*X;
OUT_2_1 = ifft(output_2_1(1:0.5*length(output_2_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_2_1 = output_2_1(0.5*length(output_2_1)+1:length(output_2_1));
%fast convolve h2 with N - 2N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_2_2 = IR.*X;
OUT_2_2 = ifft(output_2_2(1:0.5*length(output_2_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_2_2 = output_2_2(0.5*length(output_2_2)+1:length(output_2_2));
%fast convolve h3 with 1 - 2N samples.
IR = fft(h(4*N:6*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_12_3 = IR.*X;
OUT_12_3 = ifft(output_12_3(1:0.5*length(output_12_3)));
%carry over from FFT math previously (overlap add portion)
carry_over_12_3 = output_12_3(0.5*length(output_12_3)+1:length(output_12_3));
%fast convolve h4 with 1 - 2N samples.
IR = fft(h(6*N:8*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_12_4 = IR.*X;
OUT_12_4 = ifft(output_12_4(1:0.5*length(output_12_4)));
%carry over from FFT math previously (overlap add portion)
carry_over_12_4 = output_12_4(0.5*length(output_12_4)+1:length(output_12_4));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 1N to 3N samples with h0
output = conv(x((block_counter - 1)*N + 1 : N+block_Counter*N),h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with 2N - 3N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_3_1 = IR.*X;
OUT_3_1 = ifft(output_3_1(1:0.5*length(output_3_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_3_1 = output_3_1(0.5*length(output_3_1)+1:length(output_3_1));
%fast convolve h2 with 2N - 3N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_3_2 = IR.*X;
OUT_3_2 = ifft(output_3_2(1:0.5*length(output_3_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_3_2 = output_3_2(0.5*length(output_3_2)+1:length(output_3_2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ceiling Reached. Algorithm gets somewhat repititive from here on in. In
% this particular example, every 4 blocks of code will look very similar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 2N to 4N samples with h0
output = conv(x((block_counter - 1)*N + 1 : N+block_Counter*N),h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with 3N - 4N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_4_1 = IR.*X;
OUT_4_1 = ifft(output_4_1(1:0.5*length(output_4_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_4_1 = output_4_1(0.5*length(output_4_1)+1:length(output_4_1));
%fast convolve h2 with 3N - 4N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_4_2 = IR.*X;
OUT_4_2 = ifft(output_4_2(1:0.5*length(output_4_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_4_2 = output_4_2(0.5*length(output_4_2)+1:length(output_4_2));
%fast convolve h3 with 2N - 4N samples
IR = fft(h(4*N:6*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_34_3 = IR.*X;
OUT_34_3 = ifft(output_34_3(1:0.5*length(output_34_3)));
%carry over from FFT math previously (overlap add portion)
carry_over_34_3 = output_34_3(0.5*length(output_34_3)+1:length(output_34_3));
%fast convolve h4 with 2N - 4N samples
IR = fft(h(6*N:8*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_34_4 = IR.*X;
OUT_34_4 = ifft(output_34_4(1:0.5*length(output_34_4)));
%carry over from FFT math previously (overlap add portion)
carry_over_34_4 = output_34_4(0.5*length(output_34_4)+1:length(output_34_4));
%fast convolve h5 with 1 - 4N samples
IR = fft(h(8*N:12*N), 8*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 8*N - 1);
output_1234_5 = IR.*X;
OUT_1234_5 = ifft(output_1234_5(1:0.5*length(output_1234_5)));
%carry over from FFT math previously (overlap add portion)
carry_over_1234_5 = output_1234_5(0.5*length(output_1234_5)+1:length(output_1234_5));
%fast convolve h6 with 1 - 4N samples
IR = fft(h(12*N:16*N), 8*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 8*N - 1);
output_1234_6 = IR.*X;
OUT_1234_6 = ifft(output_1234_6(1:0.5*length(output_1234_6)));
%carry over from FFT math previously (overlap add portion)
carry_over_1234_6 = output_1234_6(0.5*length(output_1234_6)+1:length(output_1234_6));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 3N to 5N samples with h0
output = conv(x((block_counter - 1)*N + 1 : N+block_Counter*N),h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with 4N - 5N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_5_1 = IR.*X;
OUT_5_1 = ifft(output_5_1(1:0.5*length(output_5_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_5_1 = output_5_1(0.5*length(output_5_1)+1:length(output_5_1));
%fast convolve h2 with 4N - 5N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_5_2 = IR.*X;
OUT_5_2 = ifft(output_5_2(1:0.5*length(output_5_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_5_2 = output_5_2(0.5*length(output_5_2)+1:length(output_5_2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 2N to 4N samples with h0
output = conv(x((block_counter - 1)*N + 1 : N+block_Counter*N),h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with 5N - 6N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_6_1 = IR.*X;
OUT_6_1 = ifft(output_6_1(1:0.5*length(output_6_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_6_1 = output_6_1(0.5*length(output_6_1)+1:length(output_6_1));
%fast convolve h2 with 5N - 6N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_6_2 = IR.*X;
OUT_6_2 = ifft(output_6_2(1:0.5*length(output_6_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_6_2 = output_6_2(0.5*length(output_6_2)+1:length(output_6_2));
%fast convolve h3 with 4N - 6N samples
IR = fft(h(4*N:6*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_56_3 = IR.*X;
OUT_56_3 = ifft(output_56_3(1:0.5*length(output_56_3)));
%carry over from FFT math previously (overlap add portion)
carry_over_56_3 = output_56_3(0.5*length(output_56_3)+1:length(output_56_3));
%fast convolve h4 with 4N - 6N samples
IR = fft(h(6*N:8*N), 4*N - 1);
X = fft(x((block_counter - 1)*N + 1 : N+block_Counter*N), 4*N - 1);
output_56_4 = IR.*X;
OUT_56_4 = ifft(output_56_4(1:0.5*length(output_56_4)));
%carry over from FFT math previously (overlap add portion)
carry_over_56_4 = output_56_4(0.5*length(output_56_4)+1:length(output_56_4));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
block_Counter = block_Counter + 1;
%convolve 5N to 7N samples with h0
output = conv(x((block_counter - 1)*N + 1 : N+block_Counter*N),h(1:2*N));
%play output
soundsc(output,96000);
%fast convolve h1 with 6N - 7N samples
IR = fft(h(2*N:3*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_7_1 = IR.*X;
OUT_7_1 = ifft(output_7_1(1:0.5*length(output_7_1)));
%carry over from FFT math previously (overlap add portion)
carry_over_7_1 = output_7_1(0.5*length(output_7_1)+1:length(output_7_1));
%fast convolve h2 with 6N - 7N samples
IR = fft(h(3*N:4*N), 2*N - 1);
X = fft(x(block_Counter*N + 1 : N+block_Counter*N), 2*N - 1);
output_7_2 = IR.*X;
OUT_7_2 = ifft(output_7_2(1:0.5*length(output_7_2)));
%carry over from FFT math previously (overlap add portion)
carry_over_7_2 = output_7_2(0.5*length(output_7_2)+1:length(output_7_2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%