Chebyshev vs. Butterworth Chebyshev vs. Butterworth Filters: Speed, Quality Factor, and Making the Right Choice
Introduction:
When it comes to selecting a filter for signal processing, Chebyshev and Butterworth filters are two of the most popular options. Both filters have their unique strengths and weaknesses, and choosing the right one can greatly impact the effectiveness of your signal processing. In this post, we'll explore why the Chebyshev filter is faster than the Butterworth filter and delve into the trade-offs associated with the quality factor of the Chebyshev filter. We'll also provide an explanation of the quality factor to help you make an informed decision.
Quality Factor: A Brief Overview
The quality factor, also known as the Q-factor, is a dimensionless parameter that represents the "sharpness" of a filter's frequency response. In other words, it measures how well a filter can separate signals with close frequencies. A higher Q-factor indicates a more selective filter, with a steeper roll-off between the passband and the stopband. A lower Q-factor, on the other hand, results in a smoother transition between the passband and the stopband.
Chebyshev vs. Butterworth: Speed and Performance
The Chebyshev filter is generally faster than the Butterworth filter due to its equiripple frequency response. This equiripple response allows the Chebyshev filter to achieve a steeper roll-off between the passband and the stopband with fewer filter coefficients. Consequently, the filter requires fewer calculations, resulting in faster signal processing.
The Butterworth filter, in contrast, is characterized by a maximally flat frequency response in the passband, which results in a slower roll-off between the passband and the stopband. This means that more filter coefficients are required to achieve the desired level of attenuation, leading to slower signal processing.
Trade-offs: Quality Factor and Filter Performance
The primary trade-off between the Chebyshev and Butterworth filters lies in the balance between the quality factor and the filter's performance. The Chebyshev filter boasts a higher quality factor, which translates to a steeper roll-off and better selectivity. However, this comes at the expense of ripples in the frequency response, which can introduce distortion or signal artifacts.
The Butterworth filter, with its maximally flat passband, provides a smoother frequency response with no ripples. This results in lower distortion and signal artifacts but a lower quality factor, which means the filter may struggle to separate closely spaced frequencies.
Is the Trade-off Worth It?
Deciding whether the trade-off between the quality factor and filter performance is worth it ultimately depends on your specific application and signal processing requirements. If your primary concern is speed and selectivity, the Chebyshev filter may be the better choice. Its higher quality factor and faster signal processing make it an excellent option for applications where steep roll-offs and rapid response times are critical.
However, if minimizing signal distortion and artifacts is more important, the Butterworth filter may be more suitable. Its smooth, ripple-free frequency response ensures a cleaner output signal, even if it comes at the cost of a slower roll-off and reduced selectivity.
Conclusion:
When choosing between the Chebyshev and Butterworth filters, it's essential to consider the balance between speed, quality factor, and filter performance. The Chebyshev filter offers a faster response and a higher quality factor, making it ideal for applications where selectivity and rapid response are crucial. However, its equiripple frequency response can introduce distortion, which may not be suitable for all applications. On the other hand, the Butterworth filter provides a smoother, ripple-free frequency response, but with a lower quality factor and slower roll-off.
Ultimately, selecting the right filter for your trading strategy depends on your specific needs and goals. In the world of trading, making timely and accurate decisions is crucial, and the filter you choose plays a significant role in achieving this. Carefully consider the trade-offs between the speed, quality factor, and filter performance when deciding between the Chebyshev and Butterworth filters. By understanding the strengths and weaknesses of each filter type, you can choose the one that best suits your trading requirements and achieve the desired results in your market analysis. Remember that the best filter choice might vary from one trading strategy to another, so always be prepared to reassess your decision based on the unique demands of each trading approach and market conditions.
Dsp
Properties And Spectral Interaction Of Zero-Lag FiltersIntroduction
In this post i want to talk about zero-lag filters, how they interact with the price and its frequency components. I'll also talk about the phase-response, and try to clearly explain how it works and what information it can give to the user. I'll finally introduce the concept of forward-backward filtering as well as zero-phase non stable causal smoothing.
Filters And Lag
Lag is a term used in technical analysis that refer to the phase-shift induced by filters. As you know filters interact with the frequency content of a signal, they can remove certain frequency components or amplify/reduce their amplitude. Lag can be perceived when smoothing market price by using a low-pass/band-pass filter, in short a filter with lag will return past-trends instead of new one, this can be considered a tradeoff where the user can access information easier to interpret at the cost of reactivity.
Phase Response
One can visualize the phase of filters thanks to the phase-response. The phase-response is a value expressed in degree or radians and is described as the relationship of a sinusoid and the phase, its a bit confusing so let me explain you how it works. Remember that a sine wave have a amplitude and a frequency and a period, she can also have a certain phase expressed in degree, for example in this image www.davidbridgen.com the sine wave in red is shifted by 180 degree, the phase response of a filter will tell you how many degree a frequency component (sinusoid) is shifted after being filtered.
Here an image showing a frequency response : i.stack.imgur.com
This is because frequency components are shifted that lag can be perceived.
Tackling The Lag Problem
So technical analyst tackled this problem by making zero-lag filters, of course the term zero-lag must be taken lightly, basically zero-lag will mean a filter who better fit to the data. So how do this work ?
Remember that a filter posses a frequency response, the frequency response tell you how the filter interact with the frequency components of the signal. So with most of the zero-lag filters lag will be reduced by amplifying some frequency components of the filter, some zero-lag filters will have the following frequency response :
This frequency response amplify certain frequencies before the transition band, this allow the filter to better fit to the signal. Of course this is not the only way to make filter have zero-lag, common zero-lag filter structures include :
amplifying certain frequencies in price -> applying filter
applying a bandpass filter to the price -> summing the result with a low-pass filter
multiply a low-pass filter with cutoff frequency a by 2 -> subtract the result to a low-pass filter of cutoff frequency b with a > b
As you can see such filters produces better fit but are less smooth than other filters of the same period, this is logical, you are amplifying certain frequencies, and some of those frequencies can be high ones, basically noise, which explain the reactivity-smoothness tradeoff. The amplification process also creates artifacts such as over/undershoots which are direct effects of amplification.
Zero-Phase Smooth Filters - Non Causal
Any filter can have literally zero-lag and be smooth by a method called forward-backward filtering, this method consist in filtering the data from the left to the right and then filtering this result from the right to the left, during the last step you basically shift back the filtered result to the right, which compensate the shift produced by the first step filtering.
Such filters work by reversing the orders of the signal samples, now they are said to be non-causal because they no longer use only past information, this is why such filters are used offline , their phase response is equal to 0. Those filters are the core of many repainting indicators.
Zero-Phase Smooth Filters - Causal
Impossible ? In theory yes, at least with FIR filters, however IIR filters can work differently. IIR filters are less stable than FIR filters and posses a non-linear phase , this mean that their phase is not a linear function. IIR filters are filters using past outputs as input, as said they can sometimes produce zero-lag smooth outputs, but those results are not stable and does not occur every time, in facts they are rare events.
An example is made by using double exponential smoothing
Using low values for beta can produce non-stables results appearing non-causal, and sometimes even great fits.
However those effects does not appear constantly. Another way to have causal zero-lag filters is to forecast the data and smooth it, however you then are affected by the accuracy of your forecast model, how unfortunate.
Conclusion
This post took more time than necessary, but it is interesting to see how zero-lag filters works from a signal-processing point of view. So from now on if you see filters appearing to good to be true, you are certainly dealing with one using forward-backward filtering, either way you can't violate causality, no matter how hard you try...its also socially inappropriate (lame jokes !!!!!) .
Thanks for reading !
Why A Cascading SMA Approximate A Gaussian Filter ?Introduction
The gaussian filter don't see many uses in technical analysis and financial data smoothing in general, however it possess really interesting properties and a really close relationship with the simple moving average.
The gaussian filter is a filter which possess a function approximately gaussian (bell shaped curve) as : impulse response, step response and frequency response. This characteristic is pretty cool actually, the gaussian function is always mysterious.
Now why do I talk about sma and estimation ? Well it is true, you can estimate a gaussian filter by applying an sma to another sma and so on such as : sma(...sma())
But why ? Just why is that so ? Well there are a lot of explanations, some of them involving the central limit theorem which would lead to a statistical explanation but I'll give a simpler explanation of this case by using signal processing.
Understanding Impulses Responses
The impulse response of a filter is the filter output using an impulse function as input or more simply : filter(impulse)
The impulse function is a simple function equal to 1 at a certain point in time, for example we can use : impulse = 1 if t = 10 else 0, where t = 1,2,3...inf
The impulse response of a filter tell us how to actually make the filter, for example :
a = filter(impulse)
b = sum(input*a) = filter(input)
This process is called convolution, and is simply the sum of the product of two functions, the input function and the kernel function, a kernel is just a way to say filter coefficients.
The Explanation
Now that you know that, let's explain why sma(...sma()) approximate a gaussian filter.
To do so let's take an impulse function and let's start applying an sma to it such as sma(impulse) (the sma period doesn't matter here)
Only one sma give a constant, let's use two sma's such as sma(sma(impulse))
This give us a triangular function, this is why sma(sma()) is often called triangular moving average, now let's repeat the process and add more sma's.
Do you see ? We are approximating a gaussian curve, if we do it many times the approximation will be even more correct.
Now let's recall :
The impulse response of a gaussian filter is a gaussian function f
The impulse response of many sma's give a function f' who approximate a gaussian function, therefore f ≈ f'
So sum(input*f') ≈ sum(input*f) and therefore sma(...sma(input)) ≈ gaussfilter(input)
Note : the process of applying a filter several time is called cascading
Conclusion
Simple isn't it ? The simple moving average is always fun to use and posses many properties, now you don't want to use such method because it's mega inefficient.
But maybe that you want to know about an efficient gaussian filter implementation ? I can work on it. Thanks for reading !
Digital Filters And DSPIntroduction
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, F inite I mpulse R esponse filters or FIR and I nfinite I mpulse R esponse 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 :)
Thanks for reading !