Wednesday, January 18, 2012

FIR filter implementation

FIR (finite impulse response) filter is a digital filter without feedback. Absence of feedback makes impulse filter response to be finite. Digital filters can be with infinite impulse response (IIR filters).
The advantages of FIR filters compared to IIR filters:
1. FIR filter can be designed with linear phase.
2. FIR filter is simple to implement.

 3. FIR filter can be easily implemented on finite-precision arithmetic (a lot of microcontrollers can operate with 16-bit words, but for IIR filter correct working, in some cases, you need 32 bits to store "Y" coefficients. So there is much more problems with IIR filter implementation on 16-bit MCU, than with FIR filter implementation).

The disadvantages of FIR filters compared to IIR filters:
1. FIR sometimes requires more memory and calculations to achieve a desired response characteristic.
2. Some responses are not practical to implement.

The equation of the FIR filter is as follows:
Input x(n) and output y(n) signals are related via the convolution operation. h(k) - filter coefficients. N is a filter length.
Block diagram of the FIR filter:
Coefficients h(n):
Input data x(n):
Let N = 3 then:
Coefficients h(n):
 
Input data x(n): 

The equation in this case is as follows: 

A detailed description of the entry and modification of data in the filter is given below:


Consider the filter implementation in C language. The input signal is a unit impulse at zero time. Thus, finding the Fourier transform of the output signal we obtain the amplitude-frequency characteristic of the filter. The length of the input signal is 1000 points. After filtering the signal, the result is stored in the file Y.dat using WinAPI functions.
int main()
{

    float X[1000] = {0};
    X[0] = 1;

    float Y[1000] = {0};


    float yn = 0;
    const int N = 3;
    float h[N] = {0.32590173795979338000,
                  0.34819652408041324000,
                  0.32590173795979338000};
    float x[N] = {0,0,0};


    for(int i=0; i<1000; i++)
    {
                                                 //  Alternative implementation
        for(int k=0; k < N-1; k++)               //  for(int k=N-1; k>0; k--)
        {                                        //  {                      
          x[N-k-1] = x[N-k-2];//shift the data   //    x[k] = x[k-1];
        }                                        //  }              

              
        x[0] = X[i]; // move input sample to buffer
        yn = 0; // clear output sample

        for(int k=0; k < N; k++)
        {
            yn += h[k]*x[k]; // multiply data on coefficients with accumulation
        }

        Y[i] = yn;
    }

    HANDLE hFile = CreateFile("Y.dat", GENERIC_READ | GENERIC_WRITE, 0, NULL, 
                               CREATE_ALWAYS, 0, NULL);

    DWORD dwCount;
    WriteFile(hFile,Y,sizeof(Y),&dwCount,NULL);
    CloseHandle(hFile);

    return 0;
}

Filter response can be plotted in MATLAB. To do this, open the file Y.dat, read the response of the filter and apply Fourier transform.
clc;
close all;
clear all;

fid = fopen('Y.dat','r'); %open file
F = fread(fid,'float');   %read file
fclose(fid);              %close file 

N = length(F);            %get signal length (number of samples)
Fs = 500;                 %set sample frequency
dt = 1/Fs;                %get sample period in time domain
df = Fs/N;                %get sample period in frequency domain
t = (1:N)*dt;             %get time vector 
f = (1:N)*df;             %get frequency vector 

plot(t,F);                %plot signal in time 

figure;
S = abs(fft(F));          %get frequency response of filter 
plot(f(1:N/2),20*log10(S(1:N/2)/max(S(1:N/2))));  %plot frequency response
xlabel('Hz');
ylabel('S, dB');

Frequency response of the filter:

Sources:
1. Emmanuel C. Ifeachor and Barrie W. Jervis Digital Signal Processing: A Practical Approach, 2nd Edition. Hardcover. from English. - Moscow: Publishing House "Williams", 2008. - 992 pp.

39 comments:

  1. I just wanted to write a brief comment in order to express gratitude to you for all of the remarkable tips and hints you are showing on this site. My extensive internet research has now been compensated with beneficial insight to write about with my family. I 'd express that we website visitors actually are truly lucky to exist in a fabulous place with so many special individuals with very beneficial hints. I feel pretty privileged to have seen the web site and look forward to tons of more entertaining moments reading here. Thanks a lot again for everything.
    SRAM 2011 Apex Road Bicycle Brake - Black

    ReplyDelete
  2. I think this is among the most significant info for me. And i'm glad reading your article. But wanna remark on some general things, The website style is ideal, the articles is really excellent : D. Good job, cheers
    La Canadienne Women's Felicia Boot

    ReplyDelete
  3. Really Nice article ........... i understand Much and very clearly...... Thanks to this article.......

    ReplyDelete
  4. really helpful and knowledgeable article.did you know how to implement IIR filters?

    ReplyDelete
  5. Thank you so much! I needed a way to refresh all those concepts.

    ReplyDelete
  6. float h[N] = {0.32590173795979338000,
    0.34819652408041324000,
    0.32590173795979338000};

    what are this numbers in array, how to select this value.

    it is fixed of it can be vary.

    i did not get this part of code, pls wxplain if possible

    ReplyDelete

  7. hello good night, look what happens is that I design a FIR filter kaiser window in Matlab and I already have the coefficients but I need to program them in arduino, then if you could help me in that. Thank you

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hello,
    why you have initialized yn=0 and then why x[o]=X[i]

    ReplyDelete
  10. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Devops Training in Bangalore
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  11. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Data Science Training in Chennai
    Data Science course in anna nagar
    Data Science course in chennai
    Data science course in Bangalore
    Data Science course in marathahalli
    Data science course in bangalore

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hi there! I simply want to offer you a huge thumbs up for your great information you have here on this post. I am coming back to your blogfor more soon.

    ReplyDelete
  14. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    salesforce training in chennai

    software testing training in chennai

    robotic process automation rpa training in chennai

    blockchain training in chennai

    devops training in chennai

    ReplyDelete
  15. Awesome blog. Thanks for sharing this blog. Keep update like this...
    Android Training in Bangalore
    Android Classes in Pune

    ReplyDelete
  16. Wonderful blog. I am delighted in perusing your articles. This is genuinely an incredible pursuit for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
    data analytics courses in hyderabad with placements

    ReplyDelete
  17. This post is so usefull and informaive keep updating with more information.....
    UI Design Course In Bangalore
    UI UX Institute In Bangalore

    ReplyDelete
  18. Great post. I used to be checking continuously this blog and I’m impressed! Extremely helpful info particularly the final section I deal with such info much. I was looking for this certain info for a long time. Thank you and best of luck. canaan avalon 1246

    ReplyDelete
  19. This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. classic furniture

    ReplyDelete
  20. I wrote about a similar issue, I give you the link to my site. Patio Chair

    ReplyDelete
  21. You've completed in excellent work. t suggest to my frtends ind personilly wtll certitnly dtgtt. t'm conftdent they'll be gitned from thts webstte sydney physiotherapists

    ReplyDelete
  22. I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan! buy website traffic

    ReplyDelete
  23. Zebra blinds, also called dual blinds, are a modern roller blind alternative to horizontal blinds. Consisting of alternating horizontal opaque and sheer fabric stripes, these blinds transition from day to night easily.

    room darkening blinds

    ReplyDelete
  24. Zebra blinds offer both light filtering and privacy protection in one roller shade. The sheer stripes provide light filtering, glare reduction, and UV protection. When the opaque stripes are overlapping, your blinds will allow for light to filter through, giving your space the brightness it needs.

    blackout shades

    ReplyDelete
  25. "I found this blog post to be incredibly informative and engaging, and it perfectly captures the essence of the topic!"
    Golang Training

    ReplyDelete