libdspl-2.0
Digital Signal Processing Algorithm Library
FIR filter design.

Functions

int DSPL_API fir_linphase (int ord, double w0, double w1, int filter_type, int win_type, double win_param, double *h)
 Function calculates linear-phase FIR filter coefficients by window method. More...
 

Detailed Description

Function Documentation

◆ fir_linphase()

int DSPL_API fir_linphase ( int  ord,
double  w0,
double  w1,
int  filter_type,
int  win_type,
double  win_param,
double *  h 
)

Function calculates linear-phase FIR filter coefficients by window method.


FIR filter transfer function is

\[ H(z) = \sum_{n = 0}^{ord} h_n z^{-n}. \]

Parameters
[in]ordFilter order.
Number of FIR filter coefficients is ord+1.

[in]w0Normalized cutoff frequency for lowpass and highpass filter, or left cutoff frequency for bandpass or bandstop filter.

[in]w1Right normalized cutoff frequency for bandpass or bandstop filter.
This parameter is ignored for lowpass or highpass filters.
Frequecny w1 must be higher than w0.

[in]filter_typeFilter type.
This parameter can be one of follow:
DSPL_FILTER_LPF   - lowpass  filter;
DSPL_FILTER_HPF   - highpass filter;
DSPL_FILTER_BPASS - bandpass filter;
DSPL_FILTER_BSTOP - bandstop filter.


[in]win_typeWindow function type.
This parameter can be one of follow:
-------------------------------------------------------------------------
 win_type                    |  Description
-----------------------------|-------------------------------------------
 DSPL_WIN_BARTLETT           | Nonparametric Bartlett window
-----------------------------|-------------------------------------------
 DSPL_WIN_BARTLETT_HANN      | Nonparametric Bartlett-Hann window
-----------------------------|-------------------------------------------
 DSPL_WIN_BLACKMAN           | Nonparametric  Blackman window 
-----------------------------|-------------------------------------------
 DSPL_WIN_BLACKMAN_HARRIS    | Nonparametric Blackman-Harris window
-----------------------------|-------------------------------------------
 DSPL_WIN_BLACKMAN_NUTTALL   | Nonparametric Blackman-Nuttall
-----------------------------|-------------------------------------------
 DSPL_WIN_CHEBY              | Parametric Dolph-Chebyshev window.
                             | Parametr  `win_param` sets sidelobe attenuation 
                             | level in dB.
-----------------------------|-------------------------------------------
 DSPL_WIN_COS                | Nonparametric Cosine window
-----------------------------|-------------------------------------------
 DSPL_WIN_FLAT_TOP           | Nonparametric maxflat window
-----------------------------|-------------------------------------------
 DSPL_WIN_GAUSSIAN           | Nonparametric Gauss window
-----------------------------|-------------------------------------------
 DSPL_WIN_HAMMING            | Nonparametric Hamming window
-----------------------------|-------------------------------------------
 DSPL_WIN_HANN               | Nonparametric Hann window
-----------------------------|-------------------------------------------
 DSPL_WIN_KAISER             | Parametric Kaiser window
-----------------------------|-------------------------------------------
 DSPL_WIN_LANCZOS            | Nonparametric Lanczos window
-----------------------------|-------------------------------------------
 DSPL_WIN_NUTTALL            | Nonparametric Nuttall window
-----------------------------|-------------------------------------------
 DSPL_WIN_RECT               | Nonparametric rectangular window
-------------------------------------------------------------------------


[in]win_paramParameter value for parametric windows.
This parameter is used for parametric windows only and is ignored for nonparametric windows.

[out]hPointer to the linear-phase FIR filter coefficients vector.
Vector size is [ord+1 x 1].
Memoru must be allocated.

Note
Only symmetric windows can achieve linear-phase FIR filter.

Bandstop filter type (filter_type = DSPL_FILTER_BSTOP) requires only even filter order ord. If filter_type = DSPL_FILTER_BSTOP and ord is odd then function returns ERROR_FILTER_ORD code.
Returns
RES_OK if filter coefficients is calculated successfully.
Else code error.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
/*filter order */
#define ORD 64
#define W0 0.3
#define W1 0.7
/* Frequency response vector size */
#define N 1024
/*******************************************************************************
* function calculates filter frequency response and save magnitude to
* the text file.
* params: b - pointer to the transfer fuction H(z) numerator vector
* a - pointer to the transfer fuction H(z) denominator vector
* ord - filter order
* n - number of magnitude vector size
* fn - file name
******************************************************************************/
void freq_resp_write2txt(double* b, int ord, int n, char* fn)
{
double *w = NULL, *mag = NULL;
int k;
w = (double*)malloc(n*sizeof(double));
mag = (double*)malloc(n*sizeof(double));
/* Normalized frequency from 0 to pi */
linspace(0, M_PI, n , DSPL_PERIODIC, w);
/* Magnitude (dB) calculation */
filter_freq_resp(b, NULL, ord, w, n, DSPL_FLAG_LOGMAG, mag, NULL, NULL);
/* Frequency normaliztion from 0 to 1 */
for(k = 0; k < N; k++)
w[k] /= M_PI;
/* Save magnitude to the txt file */
writetxt(w, mag, n, fn);
free(w);
free(mag);
}
/*******************************************************************************
* Main program
******************************************************************************/
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handle */
hdspl = dspl_load(); /* Load DSPL functions */
/* FIR filter coeff. vectors */
double h[ORD+1];
/*------------------------------------------------------------------------*/
fir_linphase(ORD, W0, W1, DSPL_FILTER_LPF, DSPL_WIN_RECT, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_lpf_rect.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_LPF, DSPL_WIN_HAMMING, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_lpf_hamming.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_LPF, DSPL_WIN_BLACKMAN, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_lpf_blackman.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_LPF, DSPL_WIN_BLACKMAN_HARRIS, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_lpf_blackman_harris.txt");
/*------------------------------------------------------------------------*/
fir_linphase(ORD, W0, W1, DSPL_FILTER_HPF, DSPL_WIN_RECT, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_hpf_rect.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_HPF, DSPL_WIN_HAMMING, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_hpf_hamming.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_HPF, DSPL_WIN_BLACKMAN, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_hpf_blackman.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_HPF, DSPL_WIN_BLACKMAN_HARRIS, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_hpf_blackman_harris.txt");
/*------------------------------------------------------------------------*/
fir_linphase(ORD, W0, W1, DSPL_FILTER_BPASS, DSPL_WIN_RECT, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bpass_rect.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BPASS, DSPL_WIN_HAMMING, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bpass_hamming.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BPASS, DSPL_WIN_BLACKMAN, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bpass_blackman.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BPASS, DSPL_WIN_BLACKMAN_HARRIS, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bpass_blackman_harris.txt");
/*------------------------------------------------------------------------*/
fir_linphase(ORD, W0, W1, DSPL_FILTER_BSTOP, DSPL_WIN_RECT, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bstop_rect.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BSTOP, DSPL_WIN_HAMMING, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bstop_hamming.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BSTOP, DSPL_WIN_BLACKMAN, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bstop_blackman.txt");
fir_linphase(ORD, W0, W1, DSPL_FILTER_BSTOP, DSPL_WIN_BLACKMAN_HARRIS, 0, h);
freq_resp_write2txt(h, ORD, N, "dat/fir_bstop_blackman_harris.txt");
/*------------------------------------------------------------------------*/
/* plotting by GNUPLOT */
gnuplot_create(argc, argv, 920, 840, "img/fir_linphase_test.png", &hplot);
gnuplot_cmd(hplot, "unset key");
gnuplot_cmd(hplot, "set grid");
gnuplot_cmd(hplot, "unset xlabel");
gnuplot_cmd(hplot, "set yrange [-130:5]");
gnuplot_cmd(hplot, "set xtics 0,1");
gnuplot_cmd(hplot, "set xtics add ('0.3' 0.3)");
gnuplot_cmd(hplot, "set xtics add ('0.7' 0.7)");
gnuplot_cmd(hplot, "set xtics add ('1' 1)");
gnuplot_cmd(hplot, "set multiplot layout 4,4 rowsfirst");
gnuplot_cmd(hplot, "set ylabel 'Magnitude, dB'");
gnuplot_cmd(hplot, "set title 'Rect win'");
gnuplot_cmd(hplot, "plot 'dat/fir_lpf_rect.txt' with lines");
gnuplot_cmd(hplot, "unset ylabel");
gnuplot_cmd(hplot, "set title 'Hamming win'");
gnuplot_cmd(hplot, "plot 'dat/fir_lpf_hamming.txt' with lines");
gnuplot_cmd(hplot, "set title 'Blackman win'");
gnuplot_cmd(hplot, "plot 'dat/fir_lpf_blackman.txt' with lines");
gnuplot_cmd(hplot, "set title 'Blackman-Harris win'");
gnuplot_cmd(hplot, "plot 'dat/fir_lpf_blackman_harris.txt' with lines");
gnuplot_cmd(hplot, "unset title");
gnuplot_cmd(hplot, "set ylabel 'Magnitude, dB'");
gnuplot_cmd(hplot, "plot 'dat/fir_hpf_rect.txt' with lines");
gnuplot_cmd(hplot, "unset ylabel");
gnuplot_cmd(hplot, "plot 'dat/fir_hpf_hamming.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_hpf_blackman.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_hpf_blackman_harris.txt' with lines");
gnuplot_cmd(hplot, "set ylabel 'Magnitude, dB'");
gnuplot_cmd(hplot, "plot 'dat/fir_bpass_rect.txt' with lines");
gnuplot_cmd(hplot, "unset ylabel");
gnuplot_cmd(hplot, "plot 'dat/fir_bpass_hamming.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_bpass_blackman.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_bpass_blackman_harris.txt' with lines");
gnuplot_cmd(hplot, "set ylabel 'Magnitude, dB'");
gnuplot_cmd(hplot, "set xlabel 'normalized frequency'");
gnuplot_cmd(hplot, "plot 'dat/fir_bstop_rect.txt' with lines");
gnuplot_cmd(hplot, "unset ylabel");
gnuplot_cmd(hplot, "plot 'dat/fir_bstop_hamming.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_bstop_blackman.txt' with lines");
gnuplot_cmd(hplot, "plot 'dat/fir_bstop_blackman_harris.txt' with lines");
gnuplot_cmd(hplot, "unset multiplot");
gnuplot_close(hplot);
/* free dspl handle */
dspl_free(hdspl);
return 0;
}

This function calculates coeffictiens of lowpass, highpass, bandpass and bandstop linear-phase FIR filters by using different kind of windows. Also program calculates filter magnitudes and plots.

Author
Sergey Bakhurin www.dsplib.org

Definition at line 321 of file filter_fir.c.

void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot.c:322
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot.c:387
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: inout.c:464
int DSPL_API filter_freq_resp(double *b, double *a, int ord, double *w, int n, int flag, double *mag, double *phi, double *tau)
Magnitude, phase response and group delay vectors calculation for digital or analog filter correspond...
Definition: filter_an.c:238
int DSPL_API linspace(double x0, double x1, int n, int type, double *x)
Function fills a vector with n linearly spaced elements between x0 and x1.
Definition: array.c:1009
int DSPL_API fir_linphase(int ord, double w0, double w1, int filter_type, int win_type, double win_param, double *h)
Function calculates linear-phase FIR filter coefficients by window method.
Definition: filter_fir.c:321
int DSPL_API gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
Definition: gnuplot.c:200