Digital signal processing (dsp) is used to manipulate signals and extract information from them. Among all the tools available, digital filters are the most common ones to use, in fact you are certainly using one during your analysis. In this post i will share some of the knowledge i have associated with filters and their properties.
I will try to be the more simple possible with my explanations for those with a light mathematical background :)
Before starting talking about filters, lets see some elementary things about dsp, this will help you understand what we will see next.
Time Domain
It is possible to see signals in different domains. The time domain show the signal with respect to time. For example when you see a price chart you see the price with respect to time, so you are on the time domain. A signal in the time domain is "one dimensional" because there is only one independent variables.
Frequency Domain
Frequency domain is another way to see a signal, this show the signal with respect to a frequency, in fact you are seeing the frequency content of the signal. In order to understand this i will introduce you to another powerful tool of digital signal processing, the "Fourier Transform". Lets look at some graphical explanations.
Lets take a signal we will call S
This signal is actually the sum of 3 Sine waves of different Frequencies
The one in red have a frequency of 0.07, the one in yellow have a frequency of 0.04 and the one in purple a frequency of 0.02, frequency is defined as the number of cycles in 1 unit time and is expressed in hertz Hz, here we will use "price bar" as unit time, it will be easier to see it this way. The Period is the reciprocal of the frequency, its the number of price bars in 1 cycle. To convert a frequency to a period just divide 1 by the frequency. The amplitude is the maximum absolute value of the signal, often said peak amplitude, for our sine waves the amplitude is 1.
So what does the Fourier transform ? Well the Fourier transform convert a signal in the time domain to the frequency domain. In other terms it decompose our signal in a sum of sine waves of different frequency and amplitude.In our signal S its Fourier transform would look like this :
Each bar is called a frequency bin or frequency component, they show the frequency and amplitude of one sine wave, each color represent the color of each sine waves i showed earlier. The abscissa x represent the frequency, the ordinate y represent the amplitude. Frequency bins on the left have a lower frequency than those in the right. If we sum each sine waves with the frequency and amplitude each bins give us we would get back our signal S
Its important to know this since filters interact with the frequency content, and the Fourier transform can tell us how.
What Is A Filter ?
A filter is processing tool that allow us to modify the frequency content of a signal, this mean that if we filter a signal, we will remove or change the amplitude of each frequency bin.
Interaction With The Frequency Domain
We will later see filters in the time domain but lets see them in frequency domain for the moment. In order to see how a filter interact with the frequency content of a signal we must look at the frequency response of the filter. A frequency response look like this :
The frequency response is shown in the frequency domain. So what does the red curve represent ? Well this curve show us how the filter interact with each frequency component, lets look at some terms of the frequency response.
The pass band area let each frequency components in that area unchanged, the stop band attenuation or transition band area only change the amplitude of each frequency components in that area, the stop band area remove each frequency components in that area. For example :
Here we have the frequency domain of a signal, after passing a filter to this signal the frequency domain will look like this :
In the image showed earlier you have a blue line, this line delimit the boundary between the pass band and the stop band attenuation, and this boundary is named cutoff frequency, the cutoff frequency is a value expressed in hertz. Actually when you are using a moving average, the length parameter control the position of the cutoff, but we will see moving averages later in more depth.
Types Of Filters
Now that we understand how a filter interact with frequency domain we must talk about different types of filters, filters can interact in different ways with the frequency components.
Low Pass Filters :
The name is straightforward, those filters let pass low frequency components and remove the high ones. Frequency response of low pass filters look like this :
High Pass Filters :
They let pass high frequency components while removing low ones.
Bandpass Filters :
A mix of the two previous filters, they let pass frequency components in a certain range.
Those can have two-cutoff f1, f2 with bandwidth f2 - f1 or one cutoff and one bandwidth parameter.
Band-Stop Filters :
Sometime called Notch filters or band-reject, they do the contrary of the bandpass filter, instead of letting pass frequency components in a certain range they remove them.
Design Of Different Types Of Filters
Low Pass Filters :
Low pass filters are used to remove high frequencies, when you remove high frequency components of a signal the result is a smoother signal, high frequency create irregularities in a signal and create roughness this is why the result is smoother. Low pass filters are also the core of the design of other filter types. The simplest low pass filter is called a simple moving average or running/rolling - mean/average, or boxcar filter, you will understand why the name "Boxcar" later.
The average value or mean of a signal is the sum of all values divided by the number of values, a moving average instead of using all the values will use a proportion of them.A simple moving average can be computed in different ways, here are the easiest ones.
1/n * sum(S,n)
sum(1/n * S,n)
sum(S,n)/n
change(cum(S),n)/n
where S is the signal we want to filter and n the period or number of data points we want to average.
The frequency response of a simple moving average look like this :
The higher the period the more little bumps in the stop band will be created, this is why the moving average still appear a bit rough, its because not all the frequencies in the stop band are removed, some are just attenuated. Those "bumps" are called ripples, when they are situated in the pass band we call them pass band ripples, when they are in the stop band we call them stop band ripples. We will later see filters who does not have ripples.
High Pass Filters
They remove low frequencies of a signal, its really easy to calculate those, the only thing you have to do is subtract your signal S with a low pass filter, the result show all the signal who have been filtered by the low pass filters. The frequency response of the low pass filter is then reversed. Here the frequency response of a high pass moving average filter :
They are really useful because its quite hard to forecast trends in a signal, but its way easier to forecast mid term cycles.
Bandpass Filters
They let pass frequencies in a range and they are also really easy to compute, first compute a high-pass filter by subtracting the price to a moving average low pass filter, then apply a low pass filter to the result and you end up with a bandpass filter, easy isn't it ? When creating a bandpass filter you can get the pictures of the frequency response of the filter during each step.
First step -> high pass filtering
Second Step -> Low pass filtering of the first step result
Band-Stop Filter
They are the opposite of the bandpass filter, so in order to construct those you just need you signal S and your signal S filtered by a bandpass filter. Subtract your signal S with the bandpass filtered version and you get a band-stop filter.
Filter Lag
Lag is defined as the effect of filters to show past frequencies, this delay is quite unfortunate for us since a filter with zero lag would violate causality and could provide what is called "The Holy Grail". However it is possible to minimize lag, one way is to amplify a frequency range of the signal and then filter the result, a filter describing this process is the zero-lag exponential moving average in the form of :
L = (Period - 1)/2 Y0 = S + (S - S(L)) -> Amplification Process Y1 = EMA(Y0,Period) -> Filtering Process
Other methods exist but at the end getting optimal smoothing with minimum lag is not something classical filters can do. This is why adaptive filters such as Kalman and Wiener filters where made, but this is another subject that is more complicated.
Impulse Response
We are entering a really interesting topic, the topic of impulses responses and convolution. First what is an impulse ? An impulse is just defined as a really brief signal, here an example
The impulse response denoted h(t) is the reaction of a system to an impulse, so the impulse response of a filter is the filter using an impulse an input. Now lets see the impulse response of a simple moving average of period 5 (n = 5), you will understand why they are called boxcar filters.
its just a "box", puns apart there is also something more than incredible with impulse responses, do you remember how to construct a moving average ? One of the ways was : sum(1/n * S,n) , 1/5 = 0.2, now look at the impulse response from closer
Its 0.2, or 1/5, nice isn't it ? When the impulse is equal to 1, the impulse response of a filter h(t) give us values called filter coefficients and this introduce the concept of convolution.
Convolution
The subject is vast but in resume convolution is the process of combining two function to create a third function. The symbol of convolution is ∗ not to be confounded with * who is used for multiplication.
To make it easier to understand convolution lets take back our 5 period moving average case and its impulse response h(t). Now lets make an operation describing convolution
S ∗ h(t) = sum(S * h(t),5)
This operation give us a moving average of period 5. So impulse responses can give you really useful information about your filter.
IIR Filters
There are 2 filter family, Finite Impulse Response filters or FIR and Infinite Impulse Response filters. IIR filters use recursion, this mean that they use output values as input, if we check the impulse response of those filter h(t) you will see that h(t) goes on forever after the impulse, this is why they are called infinite impulse response. The most well known IIR filter is the exponential moving average (ema), or exponential averager which is described as :
y = aS(t) + (1-a)y(t-1)
where a is called a smoothing constant and 0 < a < 1.
An IIR filter can also be in the more common form :
y(t) = a0 * S(t) + a1 * S(t-1) + b1 * y(t-1)
Advanced Filter Design
Based on which filter you want to design (FIR/IIR) you will use different methods. FIR filters are easier to design, first select what frequency response your filter should have, then compute the Fourier transform of this frequency response, this will give you the impulse response and therefore the coefficients of the filter you want to design, then you only need to apply convolution. So basically, the Fourier transform of the filter impulse response give you the frequency response of the filter and vice versa.
IIR filters are way more complicated to design, you will need the z-transform and this is something that i don't really know about, how unfortunate.
Conclusions
I hope it wasn't that complicated because it shouldn't be, of course you can make filters without knowing all this, but its always cool to have more information. If you don't understand something or think i have made an error somewhere don't hesitate to mention it in the comment :)
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.