// create spgram object // _nfft : FFT size // _wtype : window type, e.g. LIQUID_WINDOW_HAMMING // _window_len : window length // _delay : delay between transforms, _delay > 0 SPGRAM() SPGRAM(_create)(unsigned int _nfft, int _wtype, unsigned int _window_len, unsigned int _delay) { // validate input if (_nfft < 2) { fprintf(stderr,"error: spgram%s_create(), fft size must be at least 2\n", EXTENSION); exit(1); } else if (_window_len > _nfft) { fprintf(stderr,"error: spgram%s_create(), window size cannot exceed fft size\n", EXTENSION); exit(1); } else if (_window_len == 0) { fprintf(stderr,"error: spgram%s_create(), window size must be greater than zero\n", EXTENSION); exit(1); } else if (_wtype == LIQUID_WINDOW_KBD && _window_len % 2) { fprintf(stderr,"error: spgram%s_create(), KBD window length must be even\n", EXTENSION); exit(1); } else if (_delay == 0) { fprintf(stderr,"error: spgram%s_create(), delay must be greater than 0\n", EXTENSION); exit(1); } // allocate memory for main object SPGRAM() q = (SPGRAM()) malloc(sizeof(struct SPGRAM(_s))); // set input parameters q->nfft = _nfft; q->wtype = _wtype; q->window_len = _window_len; q->delay = _delay; q->frequency = 0; q->sample_rate= -1; // set object for full accumulation SPGRAM(_set_alpha)(q, -1.0f); // create FFT arrays, object q->buf_time = (TC*) malloc((q->nfft)*sizeof(TC)); q->buf_freq = (TC*) malloc((q->nfft)*sizeof(TC)); q->psd = (T *) malloc((q->nfft)*sizeof(T )); q->fft = FFT_CREATE_PLAN(q->nfft, q->buf_time, q->buf_freq, FFT_DIR_FORWARD, FFT_METHOD); // create buffer q->buffer = WINDOW(_create)(q->window_len); // create window q->w = (T*) malloc((q->window_len)*sizeof(T)); unsigned int i; unsigned int n = q->window_len; float beta = 10.0f; float zeta = 3.0f; for (i=0; i<n; i++) { switch (q->wtype) { case LIQUID_WINDOW_HAMMING: q->w[i] = hamming(i,n); break; case LIQUID_WINDOW_HANN: q->w[i] = hann(i,n); break; case LIQUID_WINDOW_BLACKMANHARRIS: q->w[i] = blackmanharris(i,n); break; case LIQUID_WINDOW_BLACKMANHARRIS7: q->w[i] = blackmanharris7(i,n); break; case LIQUID_WINDOW_KAISER: q->w[i] = kaiser(i,n,beta,0); break; case LIQUID_WINDOW_FLATTOP: q->w[i] = flattop(i,n); break; case LIQUID_WINDOW_TRIANGULAR: q->w[i] = triangular(i,n,n); break; case LIQUID_WINDOW_RCOSTAPER: q->w[i] = liquid_rcostaper_windowf(i,n/3,n); break; case LIQUID_WINDOW_KBD: q->w[i] = liquid_kbd(i,n,zeta); break; default: fprintf(stderr,"error: spgram%s_create(), invalid window\n", EXTENSION); exit(1); } } // scale by window magnitude, FFT size float g = 0.0f; for (i=0; i<q->window_len; i++) g += q->w[i] * q->w[i]; g = M_SQRT2 / ( sqrtf(g / q->window_len) * sqrtf((float)(q->nfft)) ); // scale window and copy for (i=0; i<q->window_len; i++) q->w[i] = g * q->w[i]; // reset the spgram object q->num_samples_total = 0; q->num_transforms_total = 0; SPGRAM(_reset)(q); // return new object return q; }
/* Design FIR filter using the Window method n filter length must be odd for HP and BS filters w buffer for the filter taps (must be n long) fc cutoff frequencies (1 for LP and HP, 2 for BP and BS) 0 < fc < 1 where 1 <=> Fs/2 flags window and filter type as defined in filter.h variables are ored together: i.e. LP|HAMMING will give a low pass filter designed using a hamming window opt beta constant used only when designing using kaiser windows returns 0 if OK, -1 if fail */ TfirFilter::_ftype_t* TfirFilter::design_fir(unsigned int *n, _ftype_t* fc, int type, int window, _ftype_t opt) { unsigned int o = *n & 1; // Indicator for odd filter length unsigned int end = ((*n + 1) >> 1) - o; // Loop end unsigned int i; // Loop index _ftype_t k1 = 2 * _ftype_t(M_PI); // 2*pi*fc1 _ftype_t k2 = 0.5f * (_ftype_t)(1 - o);// Constant used if the filter has even length _ftype_t k3; // 2*pi*fc2 Constant used in BP and BS design _ftype_t g = 0.0f; // Gain _ftype_t t1,t2,t3; // Temporary variables _ftype_t fc1,fc2; // Cutoff frequencies // Sanity check if(*n==0) return NULL; fc[0]=limit(fc[0],_ftype_t(0.001),_ftype_t(1)); if (!o && (type==TfirSettings::BANDSTOP || type==TfirSettings::HIGHPASS)) (*n)++; _ftype_t *w=(_ftype_t*)aligned_calloc(sizeof(_ftype_t),*n); // Get window coefficients switch(window){ case(TfirSettings::WINDOW_BOX): boxcar(*n,w); break; case(TfirSettings::WINDOW_TRIANGLE): triang(*n,w); break; case(TfirSettings::WINDOW_HAMMING): hamming(*n,w); break; case(TfirSettings::WINDOW_HANNING): hanning(*n,w); break; case(TfirSettings::WINDOW_BLACKMAN): blackman(*n,w); break; case(TfirSettings::WINDOW_FLATTOP): flattop(*n,w); break; case(TfirSettings::WINDOW_KAISER): kaiser(*n,w,opt); break; default: { delete []w; return NULL; } } if(type==TfirSettings::LOWPASS || type==TfirSettings::HIGHPASS){ fc1=*fc; // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25f; k1 *= fc1; if(type==TfirSettings::LOWPASS){ // Low pass filter // If the filter length is odd, there is one point which is exactly // in the middle. The value at this point is 2*fCutoff*sin(x)/x, // where x is zero. To make sure nothing strange happens, we set this // value separately. if (o){ w[end] = fc1 * w[end] * 2.0f; g=w[end]; } // Create filter for (i=0 ; i<end ; i++){ t1 = (_ftype_t)(i+1) - k2; w[end-i-1] = w[*n-end+i] = _ftype_t(w[end-i-1] * sin(k1 * t1)/(M_PI * t1)); // Sinc g += 2*w[end-i-1]; // Total gain in filter } } else{ // High pass filter //if (!o) // High pass filters must have odd length // return -1; w[end] = _ftype_t(1.0 - (fc1 * w[end] * 2.0)); g= w[end]; // Create filter for (i=0 ; i<end ; i++){ t1 = (_ftype_t)(i+1); w[end-i-1] = w[*n-end+i] = _ftype_t(-1 * w[end-i-1] * sin(k1 * t1)/(M_PI * t1)); // Sinc g += ((i&1) ? (2*w[end-i-1]) : (-2*w[end-i-1])); // Total gain in filter } } } if(type==TfirSettings::BANDPASS || type==TfirSettings::BANDSTOP){ fc1=fc[0]; fc2=limit(fc[1],_ftype_t(0.001),_ftype_t(1)); // Cutoff frequencies must be < 1.0 where 1.0 <=> Fs/2 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25f; fc2 = ((fc2 <= 1.0) && (fc2 > 0.0)) ? fc2/2 : 0.25f; k3 = k1 * fc2; // 2*pi*fc2 k1 *= fc1; // 2*pi*fc1 if(type==TfirSettings::BANDPASS){ // Band pass // Calculate center tap if (o){ g=w[end]*(fc1+fc2); w[end] = (fc2 - fc1) * w[end] * 2.0f; } // Create filter for (i=0 ; i<end ; i++){ t1 = (_ftype_t)(i+1) - k2; t2 = _ftype_t(sin(k3 * t1)/(M_PI * t1)); // Sinc fc2 t3 = _ftype_t(sin(k1 * t1)/(M_PI * t1)); // Sinc fc1 g += w[end-i-1] * (t3 + t2); // Total gain in filter w[end-i-1] = w[*n-end+i] = w[end-i-1] * (t2 - t3); } } else{ // Band stop //if (!o) // Band stop filters must have odd length // return -1; w[end] = _ftype_t(1.0 - (fc2 - fc1) * w[end] * 2.0); g= w[end]; // Create filter for (i=0 ; i<end ; i++){ t1 = (_ftype_t)(i+1); t2 = _ftype_t(sin(k1 * t1)/(M_PI * t1)); // Sinc fc1 t3 = _ftype_t(sin(k3 * t1)/(M_PI * t1)); // Sinc fc2 w[end-i-1] = w[*n-end+i] = w[end-i-1] * (t2 - t3); g += 2*w[end-i-1]; // Total gain in filter } } } // Normalize gain g=1/g; for (i=0; i<*n; i++) w[i] *= g; return w; }