OPEN-SOURCE SCRIPT

Template For Custom FIR Filters - Make Your Moving Average

Introduction

FIR filters (finite impulse response) are widely used in technical analysis, there is the simple or arithmetic moving average, the triangular, the weighted, the least squares...etc. A FIR filter is characterized by the fact that its impulse response (the output of a filter using an impulse as input) is finite, this mean that the impulse response won't have infinite outputs unlike IIR filters.

They are extremely simple to design to, even without the Fourier transform, this is why i post this template that will let you create custom filters from step responses. Don't hesitate to post your results.

How It Works

Originally you create your filters from the frequency response you want your filter to have, this is because the inverse Fourier transform of the frequency response is the filter impulse response.

After that step you use convolution (convolution is the sum of the product between the signal and the impulse response) and you will have your filter. But we don't have Fourier transforms in pine so how can we possibly make FIR filters from convolution ? Well here the thing, the impulse response is the derivative of the step response and the step response is the sum of the impulse response, this mean we can create filters from step responses.

cuplikan

Step response of a moving average.

Step responses are easy to design, you just need a function that start at 0 and end up at 1.

How To Use The Template

All the work is done for you, the only thing you need to do is to enter your function at line 5 :

f(x)=> your function

For example if you want your filter to have a step response equal to sqrt(x) just enter :

f(x)=> sqrt(x)

This will give the following filter output :

cuplikan

You can create custom step responses from online graphing tools like fooplot or wolfram alpha, i recommend fooplot.

You can also design your filter step response from the line 14/15/16, b will be your filter step response, just use a, for example b = pow(a,2), then replace output in plot by b and use overlay false, you can also plot step, if you like your step response copy the content of b and paste after f(x) =>.

Filter Characteristics

The impulse response determine how many of a certain signal you want in your filter, this is also called weighting, you can think of filter design as cooking where your ingredients are the the signal at different periods and the impulse response determine how many of an ingredient you must include in the recipe. The step response can also tell you about your filter characteristics, for example :

cuplikan

This one converge faster to the step function, this mean that the filter will have less lag.

cuplikan

However this one converge slower to the step function, this mean the filter might have more lag but could be smoother.

Be aware that you must find a good weighting balance, else you can have output equals to the signal or just a delayed version of the signal without smoothing.

Real Case

Lets design a sine weighted moving average (swma), this FIR filter use the first 180 degrees of a sine wave function as impulse response.

cuplikan

Impulse response of the swma.

We can design it from the step response without much problems, remember that the impulse response is the derivative of the step response, therefore the derivative of the step response is equal to the first 180 degrees of a sine wave, the derivative of the cosine function is a sine function, therefore :

f(x)=> .5*(1 - cos(x*pi))

cuplikan

And voila.

Designing A BandPass Filter

The bandpass filter like a low-pass and high pass filter, you can think of it as a smooth oscillator.

To design a bandpass filter your step response must be bell shaped, or starting at 0 and ending at 0, for example :

f(x)=>sin(x*pi) give :

cuplikan


Conclusion

Just use fooplot and experiment, you could get nice filters, i will try to post some using this template but it would be really nice to have other people use it. If you need further help pm me.

Thanks for reading !


bandpassdesignfilterFIRMoving Averagespinescriptsmoothingtemplate

Skrip open-source

Dengan semangat TradingView yang sesungguhnya, penulis skrip ini telah menerbitkannya sebagai sumber terbuka, sehingga para trader dapat memahami dan memverifikasinya. Hormat untuk penulisnya! Anda dapat menggunakannya secara gratis, namun penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib. Anda dapat memfavoritkannya untuk digunakan pada chart

Inggin menggunakan skrip ini pada chart?


Check out the indicators we are making at luxalgo: tradingview.com/u/LuxAlgo/
Juga di:

Pernyataan Penyangkalan