rlm@23
|
1 #include <stdio.h>
|
rlm@23
|
2 #include <stdlib.h>
|
rlm@23
|
3 #include <math.h>
|
rlm@23
|
4 #include <assert.h>
|
rlm@23
|
5 #include <string.h>
|
rlm@23
|
6 #include "SndfileWavUtil.h"
|
rlm@23
|
7
|
rlm@23
|
8 const int Taps = 9;
|
rlm@23
|
9 const int Points = 8;
|
rlm@23
|
10 const int log_Points = 3;
|
rlm@23
|
11 static int newest;
|
rlm@23
|
12 static short int x[Taps] = {0,0,0,0,0,0,0,0,0};
|
rlm@23
|
13 static short int fir(short int);
|
rlm@23
|
14
|
rlm@23
|
15 // foreward declaration. This is defined in DFT.cpp
|
rlm@23
|
16 int DFT(int dir,int m,double *x1,double *y1);
|
rlm@23
|
17
|
rlm@23
|
18 // lifted from the bluespec code
|
rlm@23
|
19 static double h[Taps] = {-0.0124,
|
rlm@23
|
20 0.0,
|
rlm@23
|
21 -0.0133,
|
rlm@23
|
22 0.0,
|
rlm@23
|
23 0.8181,
|
rlm@23
|
24 0.0,
|
rlm@23
|
25 -0.0133,
|
rlm@23
|
26 0.0,
|
rlm@23
|
27 -0.0124};
|
rlm@23
|
28
|
rlm@23
|
29 int
|
rlm@23
|
30 main (int argc, char * argv [])
|
rlm@23
|
31 {
|
rlm@23
|
32 const char* inputWavFileName;
|
rlm@23
|
33 const char* outputWavFileName;
|
rlm@23
|
34
|
rlm@23
|
35 FILE *inputPcmFile;
|
rlm@23
|
36 FILE *firPcmFile;
|
rlm@23
|
37 FILE *outputPcmFile;
|
rlm@23
|
38
|
rlm@23
|
39 short int sample;
|
rlm@23
|
40 short int samples[Points];
|
rlm@23
|
41
|
rlm@23
|
42 inputWavFileName = argv[1];
|
rlm@23
|
43 outputWavFileName = argv[2];
|
rlm@23
|
44
|
rlm@23
|
45 // Convert input wav to pcm
|
rlm@23
|
46 generate_pcm(inputWavFileName, "input.pcm");
|
rlm@23
|
47
|
rlm@23
|
48 inputPcmFile = fopen("input.pcm", "r");
|
rlm@23
|
49 firPcmFile = fopen("fir.pcm", "w");
|
rlm@23
|
50
|
rlm@23
|
51 assert(inputPcmFile);
|
rlm@23
|
52 assert(firPcmFile);
|
rlm@23
|
53
|
rlm@23
|
54 newest = 0;
|
rlm@23
|
55 memset(x, 0, sizeof(x));
|
rlm@23
|
56
|
rlm@23
|
57 while(fread(&sample, 2, 1, inputPcmFile)) {
|
rlm@23
|
58 sample = fir(sample);
|
rlm@23
|
59 assert(fwrite(&sample,2,1,firPcmFile));
|
rlm@23
|
60 }
|
rlm@23
|
61
|
rlm@23
|
62 fclose(inputPcmFile);
|
rlm@23
|
63 fclose(firPcmFile);
|
rlm@23
|
64
|
rlm@23
|
65 firPcmFile = fopen("fir.pcm", "r");
|
rlm@23
|
66 outputPcmFile = fopen("output.pcm", "w");
|
rlm@23
|
67
|
rlm@23
|
68 assert(firPcmFile);
|
rlm@23
|
69 assert(outputPcmFile);
|
rlm@23
|
70
|
rlm@23
|
71 // we will do an Points point fft, and then undo it
|
rlm@23
|
72 while(true) {
|
rlm@23
|
73
|
rlm@23
|
74 int read = fread(samples, 2,Points,firPcmFile);
|
rlm@23
|
75
|
rlm@23
|
76 if(read == 0)
|
rlm@23
|
77 break;
|
rlm@23
|
78
|
rlm@23
|
79 // pad out the
|
rlm@23
|
80 if(read<Points){
|
rlm@23
|
81 for(int i = read; i < Points; i++)
|
rlm@23
|
82 samples[i] = 0;
|
rlm@23
|
83 }
|
rlm@23
|
84
|
rlm@23
|
85 double dsamples[Points];
|
rlm@23
|
86 double dimag[Points];
|
rlm@23
|
87
|
rlm@23
|
88 // This shift is performed in the HW
|
rlm@23
|
89 for(int i = 0; i < Points; i++){
|
rlm@23
|
90 dsamples[i] = (double)(samples[i]>>log_Points);
|
rlm@23
|
91 dimag[i] = 0.0;
|
rlm@23
|
92 }
|
rlm@23
|
93
|
rlm@23
|
94 DFT(1,Points,dsamples,dimag);
|
rlm@23
|
95 DFT(-1,Points,dsamples,dimag);
|
rlm@23
|
96
|
rlm@23
|
97 for(int i = 0; i < Points; i++){
|
rlm@23
|
98 samples[i] = (int)dsamples[i];
|
rlm@23
|
99 }
|
rlm@23
|
100
|
rlm@23
|
101 if(read < Points){
|
rlm@23
|
102 assert(fwrite(samples,2,read,outputPcmFile)==read);
|
rlm@23
|
103 break;
|
rlm@23
|
104 } else {
|
rlm@23
|
105 assert(fwrite(samples,2,Points,outputPcmFile)==Points);
|
rlm@23
|
106 }
|
rlm@23
|
107 }
|
rlm@23
|
108
|
rlm@23
|
109 fclose(firPcmFile);
|
rlm@23
|
110 fclose(outputPcmFile);
|
rlm@23
|
111
|
rlm@23
|
112 generate_wav("output.pcm", inputWavFileName, outputWavFileName);
|
rlm@23
|
113
|
rlm@23
|
114 }
|
rlm@23
|
115
|
rlm@23
|
116 short int
|
rlm@23
|
117 fir(short int sample)
|
rlm@23
|
118 {
|
rlm@23
|
119
|
rlm@23
|
120 x[newest] = sample;
|
rlm@23
|
121 double y = 0;
|
rlm@23
|
122
|
rlm@23
|
123 for (int k = 0; k < Taps; k++) {
|
rlm@23
|
124
|
rlm@23
|
125 short int bits = x[(newest+k)%Taps];
|
rlm@23
|
126 double x = (double)bits;
|
rlm@23
|
127 y += h[k] * x;
|
rlm@23
|
128
|
rlm@23
|
129 }
|
rlm@23
|
130
|
rlm@23
|
131 newest = (newest == 0) ? (Taps-1) : (newest-1);
|
rlm@23
|
132
|
rlm@23
|
133 short int rv = ((short int)y)&0x0000FFFF;
|
rlm@23
|
134 return rv;
|
rlm@23
|
135 }
|
rlm@23
|
136
|