Mercurial > mplayer.hg
comparison libao2/firfilter.c @ 3495:cc1c879533ee
tweaked surround lowpass filter, included some new test code
author | steve |
---|---|
date | Fri, 14 Dec 2001 21:25:49 +0000 |
parents | f1c6716e7554 |
children | f99944f9f427 |
comparison
equal
deleted
inserted
replaced
3494:fb9de639ed30 | 3495:cc1c879533ee |
---|---|
1 | |
2 #include <math.h> | |
1 | 3 |
2 static double desired_7kHz_lowpass[] = {1.0, 0.0}; | 4 static double desired_7kHz_lowpass[] = {1.0, 0.0}; |
3 static double weights_7kHz_lowpass[] = {0.1, 0.1}; | 5 static double weights_7kHz_lowpass[] = {0.2, 2.0}; |
4 | 6 |
5 double *calc_coefficients_7kHz_lowpass(int rate) | 7 double *calc_coefficients_7kHz_lowpass(int rate) |
6 { | 8 { |
7 double *result = (double *)malloc(32*sizeof(double)); | 9 double *result = (double *)malloc(32*sizeof(double)); |
8 double bands[4]; | 10 double bands[4]; |
16 return result; | 18 return result; |
17 } | 19 } |
18 | 20 |
19 #if 0 | 21 #if 0 |
20 | 22 |
21 int16_t firfilter(int16_t *buf, int pos, int len, int count, double *coefficients) | 23 static double desired_125Hz_lowpass[] = {1.0, 0.0}; |
24 static double weights_125Hz_lowpass[] = {0.2, 2.0}; | |
25 | |
26 double *calc_coefficients_125Hz_lowpass(int rate) | |
22 { | 27 { |
23 double result = 0.0; | 28 double *result = (double *)malloc(256*sizeof(double)); |
29 double bands[4]; | |
24 | 30 |
25 // Back 32 samples, maybe wrapping in buffer. | 31 bands[0] = 0.0; bands[1] = 125.0/rate; |
26 pos = (pos+len-count)%len; | 32 bands[2] = 175.0/rate; bands[3] = 0.5; |
27 // And do the multiply-accumulate | 33 |
28 while (count--) { | 34 remez(result, 256, 2, bands, |
29 result += buf[pos++] * *coefficients++; pos %= len; | 35 desired_125Hz_lowpass, weights_125Hz_lowpass, BANDPASS); |
30 } | 36 |
31 return result; | 37 return result; |
32 } | 38 } |
33 | 39 |
34 #endif | 40 #endif |
35 | 41 |
55 while (count1--) result += *ptr++ * *coefficients++; | 61 while (count1--) result += *ptr++ * *coefficients++; |
56 // wrapped part of window | 62 // wrapped part of window |
57 while (count2--) result += *buf++ * *coefficients++; | 63 while (count2--) result += *buf++ * *coefficients++; |
58 return result; | 64 return result; |
59 } | 65 } |
66 | |
67 void dump_filter_coefficients(double *coefficients) | |
68 { | |
69 int i; | |
70 fprintf(stderr, "pl_surround: Filter coefficients are: \n"); | |
71 for (i=0; (i<32); i++) { | |
72 fprintf(stderr, " [%2d]: %23.20f\n", i, coefficients[i]); | |
73 } | |
74 } | |
75 | |
76 #ifdef TESTING | |
77 | |
78 #define PI 3.1415926536 | |
79 | |
80 // For testing purposes, fill a buffer with some sine-wave tone | |
81 void sinewave(int16_t *output, int samples, int incr, int freq, double phase, int samplerate) | |
82 { | |
83 double radians_per_sample = 2*PI / ((0.0+samplerate) / freq), r; | |
84 | |
85 //fprintf(stderr, "samples=%d tone freq=%d, samplerate=%d, radians/sample=%f\n", | |
86 // samples, freq, samplerate, radians_per_sample); | |
87 r = phase; | |
88 while (samples--) { | |
89 *output = sin(r)*10000; output = &output[incr]; | |
90 r += radians_per_sample; | |
91 } | |
92 } | |
93 | |
94 // Pass various frequencies through a FIR filter and report amplitudes | |
95 void testfilter(double *coefficients, int count, int samplerate) | |
96 { | |
97 int16_t wavein[8192]; //, waveout[2048]; | |
98 int sample, samples, maxsample, minsample, totsample; | |
99 int nyquist=samplerate/2; | |
100 int freq, i; | |
101 | |
102 for (freq=25; freq<nyquist; freq+=25) { | |
103 // Make input tone | |
104 sinewave(wavein, 8192, 1, freq, 0.0, samplerate); | |
105 //for (i=0; i<32; i++) | |
106 // fprintf(stderr, "%5d\n", wavein[i]); | |
107 // Filter through the filter, measure results | |
108 maxsample=0; minsample=1000000; totsample=0; samples=0; | |
109 for (i=2048; i<8192; i++) { | |
110 //waveout[i] = wavein[i]; | |
111 sample = abs(firfilter(wavein, i, 8192, count, coefficients)); | |
112 if (sample > maxsample) maxsample=sample; | |
113 if (sample < minsample) minsample=sample; | |
114 totsample += sample; samples++; | |
115 } | |
116 // Report results | |
117 fprintf(stderr, "%5d %5d %5d %5d %f\n", freq, totsample/samples, maxsample, minsample, 10*log((totsample/samples)/6500.0)); | |
118 } | |
119 } | |
120 | |
121 #endif |