41 #include "sigpr/EST_filter.h"
42 #include "sigpr/EST_fft.h"
43 #include "EST_wave_aux.h"
45 #include "sigpr/EST_Window.h"
46 #include "EST_error.h"
59 for (j = 1; j < a.
n(); ++j)
60 s += a(j) * (float)sig.
a_safe(i - j);
62 sig.
a(i) = (short) s + res.
a(i);
71 for (i = 0; i < a.
n(); ++i)
73 r = sig.a_no_check(i);
74 for (j = 1; j < a.
n(); ++j)
80 r = sig.a_no_check(i);
81 for (j = 1; j < a.
n(); ++j)
82 r -= a.
a_no_check(j) * (float)sig.a_no_check(i - j);
103 int i, j, k, start, end, size;
115 start = (int)((
float)lpc.
t(i - 1) * in_sig.
sample_rate());
116 end = (int)((
float)lpc.
t(i + 1) * in_sig.
sample_rate());
121 lpc.
frame(filter, i);
123 if (size < filter.
n())
126 in_sig.sub_wave(in_sub, start, size);
132 int centreIndex = (int)(lpc.
t(i) * (float)in_sig.
sample_rate());
139 for (k = 0, j = start; j < end; ++j, ++k){
140 out_sig.a_no_check(j) +=
141 (int)((
float)out_sub.a_no_check(k) *
160 for (start = 0, i = 0; i < lpc.
num_frames() - 1; ++i)
162 end = int((lpc.
t(i) + lpc.
t(i + 1)) * (
float)res.
sample_rate()) / 2;
166 lpc.
frame(filter, i);
174 if (start < filter.
n())
175 for (k = start; k < end; ++k)
177 for (s = 0,j = 1; j < filter.
n(); ++j)
179 sig.a_no_check(k) = (short) s + res.a_no_check(k);
182 for (k = start; k < end; ++k)
185 for (j = 1; j < filter.
n(); ++j)
186 s += filter.
a_no_check(j) * (float)sig.a_no_check(k - j);
187 sig.a_no_check(k) = (short) s + res.a_no_check(k);
205 if (order < 0) order = 0;
206 float *buff = walloc(
float,res.
num_samples()+order);
207 float *filt = walloc(
float,order+1);
208 short *residual = res.values().
memory();
212 for (k=0; k<order; k++)
214 for (start = k, m = 0, i = 0; i < lpc.
num_frames() - 1; ++i)
216 end = int((lpc.
t(i) + lpc.
t(i + 1)) * (
float)res.
sample_rate()) / 2;
222 for (k = start; k < end; ++k,++m)
225 for (j = 1; j < n; ++j)
226 s += filt[j] * buff[k-j];
229 buff[k] = s + residual[m];
233 short *signal = sig.values().
memory();
234 for (j=0,i=order; i < k; j++,i++)
235 signal[j] = (
short)buff[i];
247 sig.
a(i, j) = (int)((
float)sig.
a(i, j) + a * last);
248 last = (float)sig(i, j);
262 x = sig.a_no_check(i, j);
263 sig.a_no_check(i, j) =
264 sig.a_no_check(i, j) - int(a * x_1);
275 out.a_no_check(0, j) = sig.a_no_check(0, j);
277 out.a_no_check(i, j) =
278 sig.a_no_check(i, j) - int(a * (
float)sig.a_no_check(i-1, j));
288 out.a_no_check(0, j) = sig.a_no_check(0, j);
290 out.a_no_check(i, j) =
291 sig.a_no_check(i, j) + int(a * (
float)sig.a_no_check(i-1, j));
302 for (i = 0; (i < h); ++i)
307 sum += c.a_no_check(j);
308 a[i] = sum /(float) k;
314 for (j = 0; j < n; ++j)
315 sum += c.a_no_check(i - h + j);
316 a[i] = sum /(float) k;
323 for (j = 0; j < k; ++j)
324 sum += c.a_no_check(i - (k/2) + j);
325 a[i] = sum /(float) k;
329 c.a_no_check(i) = (short)(a[i] + 0.5);
335 int delay_correction)
341 out_sig.set_file_type(in_sig.file_type());
343 FIRfilter(in_sig, out_sig, numerator, delay_correction);
348 const EST_FVector &numerator,
int delay_correction)
350 if (delay_correction < 0)
351 EST_error(
"Can't have negative delay !\n");
353 if (numerator.
n() <= 0)
354 EST_error(
"Can't filter EST_Wave with given filter");
369 float *in = walloc(
float,n);
370 for (i=0; i < n; ++i)
371 in[i] = (
float)in_sig.a_no_check(i);
372 float *numer = walloc(
float,numerator.
n());
373 for (i=0; i < numerator.
n(); ++i)
375 float *out = walloc(
float,n);
377 for (i = 0; i < n; ++i)
382 int jhigh=numerator.
n();
384 if(i+delay_correction >= n)
385 jlow = i + delay_correction - n + 1;
387 if(i+delay_correction - jhigh < 0)
388 jhigh = i + delay_correction;
390 for(j=jlow; j<jhigh; j++)
391 if (((i+delay_correction - j) >= 0) &&
392 ((i+delay_correction - j) < n))
393 out[i] += in[i+delay_correction - j] * numer[j];
397 out_sig.a_no_check(i) = (short)out[i];
399 out_sig.set_file_type(in_sig.file_type());
423 if((filter_order & 1) == 0){
424 cerr <<
"Requested filter order must be odd" << endl;
429 int N = fastlog2(frequency_response.
n());
430 if(frequency_response.
n() != (int)pow(
float(2.0),(float)N)){
431 cerr <<
"Desired frequency response must have dimension 2^N" << endl;
438 for(i=0;i<dummy.n();i++)
444 cerr <<
"Failed to design filter because FFT failed" << endl;
450 int mid = filter_order/2;
452 reduced_filt[mid] = filt(0);
453 for(i=1; i<=mid ;i++)
456 float window = 0.5 + 0.5 * cos(PI*(
float)i / (
float)mid);
457 reduced_filt[mid+i] = filt(i) * window;
458 reduced_filt[mid-i] = filt(i) * window;
466 EST_FVector design_high_or_low_pass_FIR_filter(
int sample_rate,
467 int cutoff_freq,
int order,
468 float gain1,
float gain2)
472 if (sample_rate <= 0){
473 cerr <<
"Can't design a FIR filter for a sampling rate of "
474 << sample_rate << endl;
481 int fft_size = (int)pow(
float(2.0), float(N));
482 while(fft_size < order*4){
484 fft_size = (int)pow(
float(2.0), float(N));
491 int normalised_cutoff = (fft_size * cutoff_freq)/sample_rate;
492 for(i=0;i<normalised_cutoff;i++){
493 freq_resp[i] = gain1;
494 freq_resp[fft_size-i-1] = gain1;
496 for(;i<fft_size/2;i++){
497 freq_resp[i] = gain2;
498 freq_resp[fft_size-i-1] = gain2;
506 return design_high_or_low_pass_FIR_filter(sample_rate,
507 freq, order, 1.0, 0.0);
512 return design_high_or_low_pass_FIR_filter(sample_rate,
513 freq, order, 0.0, 1.0);
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
void FIRlowpass_filter(const EST_Wave &in_sig, EST_Wave &out_sig, int freq, int order)
void set_sample_rate(const int n)
Set sampling rate to n
EST_FVector design_FIR_filter(const EST_FVector &frequency_response, int filter_order)
void FIRfilter(EST_Wave &in_sig, const EST_FVector &numerator, int delay_correction)
void pre_emphasis(EST_Wave &sig, float a)
float & t(int i=0)
return time position of frame i
EST_FVector design_lowpass_FIR_filter(int sample_rate, int freq, int order)
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
int num_channels() const
return number of channels in track
float & a_no_check(int i, int c=0)
static void make_window(EST_TBuffer< float > &window_vals, int size, const char *name, int window_centre)
void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig, const EST_FVector &numerator)
void lpc_filter_1(EST_Track &lpc, EST_Wave &res, EST_Wave &sig)
int num_channels() const
return the number of channels in the waveform
int num_samples() const
return the number of samples in the waveform
void lpc_filter_fast(EST_Track &lpc, EST_Wave &res, EST_Wave &sig)
short & a(int i, int channel=0)
void inv_lpc_filter_ola(EST_Wave &in_sig, EST_Track &lpc, EST_Wave &out_sig)
void FIRlowpass_double_filter(EST_Wave &in_sig, int freq, int order)
void inv_lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res)
INLINE int n() const
number of items in vector.
void FIRhighpass_filter(EST_Wave &in_sig, int freq, int order)
INLINE const T & a_no_check(int n) const
read-only const access operator: without bounds checking
void resize(int num_samples, int num_channels=EST_ALL, int set=1)
resize the waveform
short & a_safe(int i, int channel=0)
int sample_rate() const
return the sampling rate (frequency)
void FIRhighpass_double_filter(const EST_Wave &in_sig, EST_Wave &out_sig, int freq, int order)
int num_frames() const
return number of frames in track
void simple_mean_smooth(EST_Wave &c, int n)
void post_emphasis(EST_Wave &sig, float a)
void frame(EST_FVector &fv, int n, int startf=0, int nf=EST_ALL)
void lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res)
EST_FVector design_highpass_FIR_filter(int sample_rate, int freq, int order)