Realization of FIR filter based on DSP C language

Source: Internet
Author: User
Tags sin

First, the output formula of the FIR filter is:

(If you can see the digital signal processing books, it's almost forgotten ...) )

Assumption: The input signal is x (n) = sin (2*pi*f1*n/fs) + sin (2*pi*f2*n/fs)

The filter designed is a low-pass FIR filter using the Hamming window with a order of 10

You can use the Filter design tool in MATLAB to design the filter:

You can then create a C header file to obtain the desired value by designing the filter:

The C program of the FIR filter is as follows:

#include <stdio.h> #include <math.h>             #define PI    3.1415#define N                      //n is the order of the filter +1#define LEN   512int Inputdata[len];                   Assume that the length of the data is 512int outputdata[len];long int yn;                                Yn is int, the data in the operation may overflow, thus filtering fails const int B[n] = {166, 0, -1374, 0, 9453, 16279, 9453, 0, -1374, 0,  166}    //fir filter parameters, Generated by Matlab
void Main () {int f1,f2,fs,i,j;        int  *a;        f1=100;    Frequency component 1 (Hz) f2=300;    Frequency component 2 (Hz) fs=800;    Sampling frequency (Hz) for (i=0;n<len;n++) {            Inputdata[n]=sin (2*pi*f1*n/fs) + sin (2*pi*f2*n/fs);    Generate Signal        }for (j=0;j<len;j++)             //Filter by Formula {a=&inputdata[j];yn=0;                for (i=0;i<n;i++) {                     yn=yn+b[i]* (*a++);                }outputdata[j]=yn>>16;     Signal}while after filtering (1);}                

Run in a CCS environment and see the time and frequency domain graphs of inputdata and outputdata by graph (not here, tested in the school's experimental class ...) )

Its cmd file is:

-e startmemory  {        page 0:         PARAM:  org = 3000h    len = 4000h        PAGE 1:                        daram:  org = 100h    len=4000h        }                                       sections{         . Text         : >       PARAM   PAGE 0          stack          : >       daram   Page 1         filter_vars: >       daram   page 1        . BSS   : >       daram   page 1        . Data  : >       Daram   PAGE 1        }

  

Realization of FIR filter based on DSP C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.