修訂 | 8bdc0852d2ff7efb566358f75572272fe638fe50 (tree) |
---|---|
時間 | 2014-06-29 23:24:16 |
作者 | suikan <suikan@user...> |
Commiter | suikan |
FIRフィルタまで動作検証済み
@@ -9,23 +9,38 @@ | ||
9 | 9 | #include <math.h> |
10 | 10 | #include <stdio.h> |
11 | 11 | #define CUTOFF (0.475) |
12 | +#define TAPS 127 | |
12 | 13 | |
13 | 14 | /* x : [0, 1.0) */ |
14 | 15 | double hanning(double x); |
15 | 16 | /* cutoff : normalized frequency where cutoff freq / sampling freq */ |
16 | 17 | double sinc(double x, double cutoff); |
18 | + /* put a sample into the delayline */ | |
19 | +void putSample( double aSample, double aDelayline[] ); | |
20 | + /* calc the convolution */ | |
21 | +double convolute ( double aDelayline[], double offset ); | |
17 | 22 | |
18 | 23 | double h( double x, int lenght); |
19 | 24 | |
25 | +double delayline[TAPS]; | |
26 | + | |
27 | + | |
28 | + | |
20 | 29 | int main(void) |
21 | 30 | { |
22 | 31 | |
23 | - int i; | |
32 | + int readsize; | |
33 | + float aSample; | |
24 | 34 | |
25 | - for ( i=0; i<127; i++) | |
35 | + while ( 1) | |
26 | 36 | { |
27 | -// printf( "%f\n", sinc(i, CUTOFF)); | |
28 | - printf( "%f\n", h( i, 127)); | |
37 | + readsize = fread((void *)&aSample, sizeof(float), 1, stdin); | |
38 | + if ( readsize < 1) | |
39 | + break; | |
40 | + putSample(aSample, delayline); | |
41 | + aSample = convolute(delayline, 0.25); | |
42 | + fwrite( &aSample, sizeof(float), 1, stdout ); | |
43 | + | |
29 | 44 | } |
30 | 45 | |
31 | 46 | return 0; |
@@ -36,14 +51,10 @@ int main(void) | ||
36 | 51 | double h( double x, int length) |
37 | 52 | { |
38 | 53 | int center; |
39 | - double fractional; | |
40 | - | |
41 | - fractional = x - floor(x); | |
42 | 54 | |
43 | 55 | center = (length -1 )/2; |
44 | 56 | |
45 | - return sinc( x - center, CUTOFF ); | |
46 | -// return hanning ( x / ( length - 1 )); | |
57 | + return sinc( x - center, CUTOFF )* hanning ( x / ( length - 1 )); | |
47 | 58 | } |
48 | 59 | |
49 | 60 | double sinc(double x, double cutoff ) |
@@ -58,3 +69,26 @@ double hanning(double x) | ||
58 | 69 | else |
59 | 70 | return 0.0; |
60 | 71 | } |
72 | + | |
73 | + | |
74 | +void putSample( double aSample, double aDelayline[] ) | |
75 | +{ | |
76 | + int i; | |
77 | + | |
78 | + /* Shift the sample in the line */ | |
79 | + for ( i=TAPS-1; i>0; i--) | |
80 | + aDelayline[i] = aDelayline[i-1]; | |
81 | + | |
82 | + aDelayline[0] = aSample; | |
83 | +} | |
84 | + | |
85 | +double convolute ( double aDelayline[], double offset ) | |
86 | +{ | |
87 | + int i; | |
88 | + double sum = 0.0; | |
89 | + | |
90 | + for ( i=0; i< TAPS; i++ ) | |
91 | + sum += aDelayline[i] * h(i+offset, TAPS); | |
92 | + | |
93 | + return sum; | |
94 | +} |