File: firdespm_lowpass_example.c

package info (click to toggle)
liquid-dsp 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,216 kB
  • sloc: ansic: 115,859; sh: 3,513; makefile: 1,350; python: 274; asm: 11
file content (89 lines) | stat: -rw-r--r-- 2,722 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// firdespm_lowpass_example.c
//
// This example demonstrates a low-pass finite impulse response filter
// design using the Parks-McClellan algorithm.
//
// SEE ALSO: firdes_kaiser_example.c

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <math.h>

#include "liquid.h"

#define OUTPUT_FILENAME "firdespm_lowpass_example.m"

// print usage/help message
void usage()
{
    printf("firdespm_lowpass_example:\n");
    printf("  -h        : print usage/help\n");
    printf("  -n <len>  : filter length,              1 < n        default: 57\n");
    printf("  -f <freq> : filter cutoff frequency,    0 < f < 0.5, default: 0.2\n");
    printf("  -s <atten>: stop-band attenuation [dB], 0 < s,       default: 60\n");
}

int main(int argc, char*argv[]) {
    // options
    unsigned int n  =  57;      // filter cutoff frequency
    float        fc = 0.2f;     // filter cutoff frequency
    float        As = 60.0f;    // stop-band attenuation [dB]

    int dopt;
    while ((dopt = getopt(argc,argv,"hn:f:s:")) != EOF) {
        switch (dopt) {
        case 'h': usage();           return 0;
        case 'n': n  = atoi(optarg); break;
        case 'f': fc = atof(optarg); break;
        case 's': As = atof(optarg); break;
        default: return -1;
        }
    }
    unsigned int i;
    printf("filter design parameters\n");
    printf("  length                : %12u\n",      n);
    printf("  cutoff frequency      : %12.8f Fs\n", fc);
    printf("  stop-band attenuation : %12.3f dB\n", As);

    // design the filter
    float h[n];
    firdespm_lowpass(n,fc,As,0,h);

#if 0
    // print coefficients
    for (i=0; i<n; i++)
        printf("h(%4u) = %16.12f;\n", i+1, h[i]);
#endif

    // open output file
    FILE*fid = fopen(OUTPUT_FILENAME,"w");
    fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
    fprintf(fid,"clear all;\n");
    fprintf(fid,"close all;\n\n");
    fprintf(fid,"h_len=%u;\n", n);
    fprintf(fid,"fc=%12.4e;\n",fc);
    fprintf(fid,"As=%12.4e;\n",As);

    for (i=0; i<n; i++)
        fprintf(fid,"h(%4u) = %20.8e;\n", i+1, h[i]);

    fprintf(fid,"nfft=1024;\n");
    fprintf(fid,"H=20*log10(abs(fftshift(fft(h,nfft))));\n");
    fprintf(fid,"f=[0:(nfft-1)]/nfft-0.5;\n");
    fprintf(fid,"figure; plot(f,H,'Color',[0 0.5 0.25],'LineWidth',2);\n");
    fprintf(fid,"grid on;\n");
    fprintf(fid,"xlabel('normalized frequency');\n");
    fprintf(fid,"ylabel('PSD [dB]');\n");
    fprintf(fid,"title(['Filter design (firdespm) f_c: %.3f, S_L: %.3f, h: %u']);\n",
            fc, -As, n);
    fprintf(fid,"axis([-0.5 0.5 -As-20 10]);\n");

    fclose(fid);
    printf("results written to %s.\n", OUTPUT_FILENAME);

    printf("done.\n");
    return 0;
}