libdspl-2.0
Digital Signal Processing Algorithm Library
Windows function for filter design and spectrum analysis.

Functions

int DSPL_API window (double *w, int n, int win_type, double param)
 Window function calculation. More...
 

Detailed Description

Function Documentation

◆ window()

int window ( double *  w,
int  n,
int  win_type,
double  param 
)

Window function calculation.


The function calculates a periodic or symmetric window function according to parameter win_type.
A periodic window function is used for spectral analysis, and a symmetric window function can be used to design FIR filters.

Parameters
[in,out]wPointer to the window.
Vector size is [n x 1].
Memory must be allocated.
The calculated window function will be placed at the given address.

[in]nSize of window function w vector.

[in]win_typeCombination of flags for specifying the type of window function.
Combination of DSPL_WIN_MASK | DSPL_WIN_SYM_MASK bit masks is used to set the window type.
Bit mask DSPL_WIN_MASK sets the window type. 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
-------------------------------------------------------------------------

Bit mask DSPL_WIN_SYM_MASK sets window function symmetry:
-------------------------------------------------------------------------
 DSPL_WIN_SYM_MASK           | Description
-----------------------------|-------------------------------------------
 DSPL_WIN_SYMMETRIC          | Symmetry window (default value)
 DSPL_WIN_PERIODIC           | Periodic window
-------------------------------------------------------------------------


[in]paramWindow function parameter.
This parameter is using only to parametric window functions, and ignored for nonparametric windows.

Returns

RES_OK if window function is calculated successfully.
Else error code.

The following program calculates 64 samples window functions, draws their spectrum when using the bin indices of the discrete Fourier transform along the frequency axis.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dspl.h"
#define N 64
#define K 1024
void win_norm(double* w, int n)
{
int k;
double s = 0.0;
for(k = 0; k < n; k++)
s+=w[k];
for(k = 0; k < n; k++)
w[k] /= s;
}
void window_plot(double* w, int n, int k, int argc, char* argv[],
double ymin, double ymax,
char* png_fn, char* time_fn, char* freq_fn, char* title)
{
void* hplot; /* GNUPLOT handles */
char str[128] = {0};
double* t = NULL;
double* W = NULL;
double* f = NULL;
double fs = (double)n;
fft_t pfft = {0};
t = (double*)malloc(n*sizeof(double));
W = (double*)malloc(k*sizeof(double));
f = (double*)malloc(k*sizeof(double));
linspace(0, n, n, DSPL_PERIODIC, t);
writetxt(t, w, n, time_fn);
win_norm(w, n);
fft_mag(w, k, &pfft, fs, DSPL_FLAG_LOGMAG | DSPL_FLAG_FFT_SHIFT, W, f);
writetxt(f, W, k, freq_fn);
/* plotting 3d surface by GNUPLOT */
/* Create window 0 */
gnuplot_create(argc, argv, 820, 360, png_fn, &hplot);
gnuplot_cmd(hplot, "set grid");
memset(str,0,128);
sprintf(str, "set title '%s'", title);
gnuplot_cmd(hplot, str);
gnuplot_cmd(hplot, "set multiplot layout 1,2 rowsfirst");
gnuplot_cmd(hplot, "set xlabel 'n'");
gnuplot_cmd(hplot, "set ylabel 'w(n)'");
gnuplot_cmd(hplot, "unset key");
gnuplot_cmd(hplot, "set xrange[0:63]");
memset(str,0,128);
sprintf(str, "set yrange[%f:%f]", ymin, ymax);
gnuplot_cmd(hplot, str);
memset(str,0,128);
sprintf(str, "plot '%s' w i lc 1,'%s' w p pt 7 ps 0.5 lc 1",
time_fn, time_fn);
gnuplot_cmd(hplot, str);
gnuplot_cmd(hplot, "set xrange[-20:20]");
gnuplot_cmd(hplot, "set yrange[-130:5]");
memset(str,0,128);
gnuplot_cmd(hplot, "set xlabel 'freq [DFT bins]'");
gnuplot_cmd(hplot, "set ylabel 'W(freq), dB'");
sprintf(str, "plot '%s' w l lc 2 ", freq_fn);
gnuplot_cmd(hplot, str);
gnuplot_close(hplot);
if(t)
free(t);
if(W)
free(W);
if(f)
free(f);
}
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
hdspl = dspl_load(); /* Load DSPL function */
double w[K] = {0};
/* Rectangular window */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_RECT, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_rect.png",
"dat/win_rect_time.txt",
"dat/win_rect_freq.txt",
"Rectangular window");
/* Bartlett window (triangular)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_BARTLETT, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_bartlett.png",
"dat/win_bartlett_time.txt",
"dat/win_bartlett_freq.txt",
"Bartlett window");
/* Flat top window */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_FLAT_TOP, 0.0);
window_plot(w, N, K, argc, argv, -1.0, 5.0,
"img/win_flattop.png",
"dat/win_flattop_time.txt",
"dat/win_flattop_freq.txt",
"Flat top window");
/* Bartlett - Hann window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_BARTLETT_HANN, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_bartletthann.png",
"dat/win_bartletthann_time.txt",
"dat/win_bartletthann_freq.txt",
"Bartlett-Hann window");
/* Blackman window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_BLACKMAN, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_blackman.png",
"dat/win_blackman_time.txt",
"dat/win_blackman_freq.txt",
"Blackman window");
/* Blackman - Harris window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_BLACKMAN_HARRIS, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_blackmanharris.png",
"dat/win_blackmanharris_time.txt",
"dat/win_blackmanharris_freq.txt",
"Blackman-Harris window");
/* Blackman - Nuttall window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_BLACKMAN_NUTTALL, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_blackmannuttall.png",
"dat/win_blackmannuttall_time.txt",
"dat/win_blackmannuttall_freq.txt",
"Blackman-Nuttull window");
/* Dolph-Chebyshev window (sidelobes level is -50 dB)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_CHEBY, 50.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_cheby50.png",
"dat/win_cheby50_time.txt",
"dat/win_cheby50_freq.txt",
"Dolph-Chebyshev window (Rs = 50dB)");
/* Dolph-Chebyshev window (sidelobes level is -80 dB)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_CHEBY, 80.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_cheby80.png",
"dat/win_cheby80_time.txt",
"dat/win_cheby80_freq.txt",
"Dolph-Chebyshev window (Rs = 80dB)");
/* Dolph-Chebyshev window (sidelobes level is -120 dB)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_CHEBY, 120.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_cheby120.png",
"dat/win_cheby120_time.txt",
"dat/win_cheby120_freq.txt",
"Dolph-Chebyshev window (Rs = 120dB)");
/* Gaussian window (sigma = 0.5)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_GAUSSIAN, 0.5);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_gaussian0p5.png",
"dat/win_gaussian0p5_time.txt",
"dat/win_gaussian0p5_freq.txt",
"Gaussian window (sigma = 0.5)");
/* Gaussian window (sigma = 0.3)*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_GAUSSIAN, 0.3);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_gaussian0p3.png",
"dat/win_gaussian0p3_time.txt",
"dat/win_gaussian0p3_freq.txt",
"Gaussian window (sigma = 0.3)");
/* Hamming window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_HAMMING, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_hamming.png",
"dat/win_hamming_time.txt",
"dat/win_hamming_freq.txt",
"Hamming window");
/* Hann window*/
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_HANN, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_hann.png",
"dat/win_hann_time.txt",
"dat/win_hann_freq.txt",
"Hann window");
/* Lanczos window */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_LANCZOS, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_lanczos.png",
"dat/win_lanczos_time.txt",
"dat/win_lanczos_freq.txt",
"Lanczos window");
/* Nuttall window */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_NUTTALL, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_nuttall.png",
"dat/win_nuttall_time.txt",
"dat/win_nuttall_freq.txt",
"Nuttall window");
/* Cosine window */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_COS, 0.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_cos.png",
"dat/win_cos_time.txt",
"dat/win_cos_freq.txt",
"Cosine window");
/* Kaiser window pi * alpha = 4 */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_KAISER, 4.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_kaiser4p0.png",
"dat/win_kaiser4p0_time.txt",
"dat/win_kaiser4p0_freq.txt",
"Kaiser window (pi * alpha = 4)");
/* Kaiser window pi * alpha = 8 */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_KAISER, 8.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_kaiser8p0.png",
"dat/win_kaiser8p0_time.txt",
"dat/win_kaiser8p0_freq.txt",
"Kaiser window (pi * alpha = 8)");
/* Kaiser window pi * alpha = 12 */
window(w, N, DSPL_WIN_PERIODIC | DSPL_WIN_KAISER, 12.0);
window_plot(w, N, K, argc, argv, 0.0, 1.1,
"img/win_kaiser12p0.png",
"dat/win_kaiser12p0_time.txt",
"dat/win_kaiser12p0_freq.txt",
"Kaiser window (pi * alpha = 12)");
dspl_free(hdspl); /* free dspl handle */
return 0;
}

A personal graph is displayed for each type of window function.

Rectangular window



Nonparametric windows



Parametric Dolph-Chebyshev windows



Parametric Gaussian windows



Parametric Kaiser windows



Author
Sergey Bakhurin. www.dsplib.org

Definition at line 308 of file win.c.

int DSPL_API window(double *w, int n, int win_type, double param)
Window function calculation.
Definition: win.c:308
void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot.c:325
Fast Fourier Transform Object Data Structure.
Definition: dspl.h:227
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot.c:390
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: inout.c:491
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 gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
Definition: gnuplot.c:200