forked from Bk8/DigitalSignalProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolve.cpp
410 lines (339 loc) · 10.3 KB
/
convolve.cpp
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* CPSC501: Assignment 4
* Adam Maidens - 10044293
*
* convolve.c
* takes in a dry recording and an impulse response and produces the convolved signal
* compile with: g++ concolve.cpp -o convolve
* run at the cmd line with: ./convolve inputfile IRfile outputfile
*
* the time domain version of convolve is commented out in later version of this code
* a version of four1 given out in class and available on D2L is used instead
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <ctime>
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
using namespace std;
/* declare variables to store the parts of a wav file */
// RIFF chunk descriptor
char chunkID[4];
int chunkSize;
char format[2];
// fmt sub-chunk
char subChunk1ID[4];
int subChunk1Size;
int16_t audioFormat;
int16_t numChannels;
int sampleRate;
int byteRate;
int16_t blockAlign;
int16_t bitsPerSample;
// data sub-chunk
char subChunk2ID[4];
int subChunk2Size;
short* fileData;
int size;
float *readWav(char *filename, float *signal);
void writeWav(char *fileName, float *signal, int signalSize);
void four1(float data[], int nn, int isign);
void four1Scaling (float signal[], int N);
//void convolve(float x[], int N, float h[], int M, float y[], int P);
void overlapAdd(float *x, int N, float *h, int M, float *y, int P);
int main(int argc, char* argv[])
{
clock_t begin = clock(); // used to time program
if (argc != 4) {
printf("Usage: ./convolve inputFile IRfile outputFile");
return 0;
}
char *inputName = argv[1];
char *impulseName = argv[2];
char *outputName = argv[3];
int N, M, P; // input signal size, impulse signal size, output signal size
float *x = NULL, *h = NULL, *y = NULL; // x, h, & y arrays
x = readWav(inputName, x);
N = size;
printf("\n");
h = readWav(impulseName, h);
M = size;
printf("\n");
P = N + M - 1;
y = new float[P];
printf("\nConvolving.....\n");
overlapAdd(x, N, h, M, y, P);
printf("Convolution finished\n");
writeWav(outputName, y, P);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("Finished with time %f seconds\n", elapsed_secs);
return 0;
}
// reads in a wav file into an array
float *readWav (char *filename, float *signal)
{
ifstream inputfile(filename, ios::in | ios::binary);
inputfile.seekg(ios::beg);
inputfile.read(chunkID, 4);
inputfile.read((char*) &chunkSize, 4);
inputfile.read(format, 4);
inputfile.read(subChunk1ID, 4);
inputfile.read((char*) &subChunk1Size, 4);
inputfile.read((char*) &audioFormat, 2);
inputfile.read((char*) &numChannels, 2);
inputfile.read((char*) &sampleRate, 4);
inputfile.read((char*) &byteRate, 4);
inputfile.read((char*) &blockAlign, 2);
inputfile.read((char*) &bitsPerSample, 2);
if (subChunk1Size == 18) {
inputfile.seekg(2, ios::cur);
}
inputfile.read(subChunk2ID, 4);
inputfile.read((char*)&subChunk2Size, 4);
size = subChunk2Size / 2;
short *data = new short[size];
for (int i = 0; i < size; i++) {
inputfile.read((char *) &data[i], 2);
}
inputfile.close();
printf("Input file %s \n", filename);
short sample;
signal = new float[size];
printf("Size: %d\n", size);
for (int i = 0; i < size; i++) {
sample = data[i];
signal[i] = (sample * 1.0) / (32767); ///////
if (signal[i] < -1.0)
signal[i] = -1.0;
}
printf("Input file converted to 1 to -1 range\n");
return signal;
}
// writes a signal to a wav file
void writeWav(char *filename, float *signal, int signalSize)
{
ofstream outputfile(filename, ios::out | ios::binary);
// File corrupted without hardcoded values
char *ChunkID = "RIFF";
char *format = "WAVE";
// PCM = 18 was unnecessary
subChunk1Size = 16;
subChunk2Size = numChannels * signalSize * (bitsPerSample / 8);
chunkSize = subChunk2Size + 36;
outputfile.write(chunkID, 4);
outputfile.write((char*) &chunkSize, 4);
outputfile.write(format, 4);
outputfile.write(subChunk1ID, 4);
outputfile.write((char*) &subChunk1Size, 4);
outputfile.write((char*) &audioFormat, 2);
outputfile.write((char*) &numChannels, 2);
outputfile.write((char*) &sampleRate, 4);
outputfile.write((char*) &byteRate, 4);
outputfile.write((char*) &blockAlign, 2);
outputfile.write((char*) &bitsPerSample, 2);
outputfile.write(subChunk2ID, 4);
outputfile.write((char*)&subChunk2Size, 4);
int16_t sample;
// converting float to int between -2^15 to 2^15 - 1
for(int i = 0; i < signalSize; i++)
{
sample = (int16_t)(signal[i] * (32767));
outputfile.write((char*)&sample, 2);
}
outputfile.close();
}
// The four1 four1 from Numerical Recipes in C,
// p. 507 - 508.
// Note: changed float data types to double.
// nn must be a power of 2, and use +1 for
// isign for an four1, and -1 for the Inverse four1.
// The data is complex, so the array size must be
// nn*2. This code assumes the array starts
// at index 1, not 0, so subtract 1 when
// calling the routine (see main() below).
void four1(float data[], int nn, int isign)
{
unsigned long n, mmax, m, j, istep, i;
float wtemp, wr, wpr, wpi, wi, theta;
float tempr, tempi;
n = nn << 1;
j = 1;
for (i = 1; i < n; i += 2) {
if (j > i) {
SWAP(data[j], data[i]);
SWAP(data[j+1], data[i+1]);
}
m = nn;
while (m >= 2 && j > m) {
j -= m;
m >>= 1;
}
j += m;
}
mmax = 2;
while (n > mmax) {
istep = mmax << 1;
theta = isign * (6.28318530717959 / mmax);
wtemp = sin(0.5 * theta);
wpr = -2.0 * wtemp * wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for (m = 1; m < mmax; m += 2) {
for (i = m; i <= n; i += istep) {
j = i + mmax;
tempr = wr * data[j] - wi * data[j+1];
tempi = wr * data[j+1] + wi * data[j];
data[j] = data[i] - tempr;
data[j+1] = data[i+1] - tempi;
data[i] += tempr;
data[i+1] += tempi;
}
wr = (wtemp = wr) * wpr - wi * wpi + wr;
wi = wi * wpr + wtemp * wpi + wi;
}
mmax = istep;
}
}
// scales the numbers in a given array signal[] and stores numbers back in array
void four1Scaling (float signal[], int N)
{
int k;
int i;
for (k = 0, i = 0; k < N; k++, i+=2) {
signal[i] /= (float)N;
signal[i+1] /= (float)N;
}
}
void complexCalculation(float complexInput[],float complexIR[],float complexResult[], int size)
{
int i = 0;
int tempI = 0;
for(i = 0; i < size; i++) {
tempI = i * 2;
complexResult[tempI] = complexInput[tempI] * complexIR[tempI] - complexInput[tempI+1] * complexIR[tempI+1];
complexResult[tempI+1] = complexInput[tempI+1] * complexIR[tempI] + complexInput[tempI] * complexIR[tempI+1];
}
}
void padZeroes(float toPad[], int size)
{
memset(toPad, 0, size);
}
void unpadArray(float result[], float complete[], int size)
{
int i, j;
for(i = 0, j = 0; i < size; i++, j+=2)
{
complete[i] = result[j];
}
}
void padArray(float output[],float data[], int dataLen, int size)
{
int i, k;
for(i = 0, k = 0; i < dataLen; i++, k+=2)
{
output[k] = data[i];
output[k + 1] = 0;
}
i = k;
memset(output + k, 0, size -1);
}
void scaleSignal(float signal[], int samples)
{
float min = 0, max = 0;
int i = 0;
for(i = 0; i < samples; i++)
{
if(signal[i] > max)
max = signal[i];
if(signal[i] < min)
min = signal[i];
}
min = min * -1;
if(min > max)
max = min;
for(i = 0; i < samples; i++)
{
signal[i] = signal[i] / max;
}
}
// Uses overlap-add method of four1
void overlapAdd(float *x,int N,float * h,int M, float *y, int P)
{
int totalSize = 0;
int paddedTotalSize = 1;
totalSize = N + M - 1;
int i = 0;
while (paddedTotalSize < totalSize)
{
paddedTotalSize <<= 1;
i++;
}
printf("Padded Size: %i exp: %i\n", paddedTotalSize, i);
printf("Input size: %i\n",N );
printf("IR size: %i\n", M);
printf("Sum IR&Input size: %i\n\n", totalSize);
float *complexResult = new float[2*paddedTotalSize];
float *input = new float[2*paddedTotalSize];
float *ir = new float[2*paddedTotalSize];
padArray(input,x, N,2*paddedTotalSize);
padArray(ir,h, M, 2*paddedTotalSize);
padZeroes(complexResult, 2*paddedTotalSize);
four1(input-1, paddedTotalSize, 1);
four1(ir-1, paddedTotalSize, 1);
printf("Complex calc\n");
complexCalculation(input, ir, complexResult, paddedTotalSize);
printf("Inverse four1\n");
four1(complexResult-1, paddedTotalSize, -1);
printf("Scaling\n");
four1Scaling(complexResult, paddedTotalSize);
unpadArray(complexResult, y, P);
scaleSignal(y, P);
}
/* the function convolve is from convolve.c
* originally given out in class and available on D2l
*/
/*****************************************************************************
*
* Function: convolve
*
* Description: Convolves two signals, producing an output signal.
* The convolution is done in the time domain using the
* "Input Side Algorithm" (see Smith, p. 112-115).
*
* Parameters: x[] is the signal to be convolved
* N is the number of samples in the vector x[]
* h[] is the impulse response, which is convolved with x[]
* M is the number of samples in the vector h[]
* y[] is the output signal, the result of the convolution
* P is the number of samples in the vector y[]. P must
* equal N + M - 1
*
*****************************************************************************/
/*
void convolve(float x[], int N, float h[], int M, float y[], int P)
{
int n, m;
// Make sure the output buffer is the right size: P = N + M - 1
if (P != (N + M - 1)) {
printf("Output signal vector is the wrong size\n");
printf("It is %-d, but should be %-d\n", P, (N + M - 1));
printf("Aborting convolution\n");
return;
}
// Clear the output buffer y[] to all zero values
for (n = 0; n < P; n++)
y[n] = 0.0;
// Do the convolution
// Outer loop: process each input value x[n] in turn
for (n = 0; n < N; n++) {
// Inner loop: process x[n] with each sample of h[]
for (m = 0; m < M; m++)
y[n+m] += x[n] * h[m];
}
}
*/