Posted By: johnnyp
Bug in Highpass2 filter - 01/03/18 20:39
The highpass2 code produces a highpass filter with the wrong critical frequency. For example, highpass2(data, 10) produces a filter with critical period of 18 bars.
The result is that roof(24, 18) gives a max frequency response at a 36.6 bar period.
Ehlers himself said that roof(24, 18) is a good alternative to a bandpass filter centered at a 20 bar period. But if you use Zorro's code, then you will get the rough equivalent of a bandpass centered on a 36 bar period.
The problem seems to be on the following line...
which would be better if it said...
Here is the python script that I use to plot the transfer responses.
Save it as script.py and call it as follows. You will need recent versions of scipy, matplotlib and numpy installed.
To plot the transfer response of the built-in highpass2 filter with cutoff = 10...
To plot the transfer response of the built-in roof filter with cutoffs 20 & 10...
To plot both in one go...
Add as many filter names and parameters as desired. For example...
Included filters...
The result is that roof(24, 18) gives a max frequency response at a 36.6 bar period.
Ehlers himself said that roof(24, 18) is a good alternative to a bandpass filter centered at a 20 bar period. But if you use Zorro's code, then you will get the rough equivalent of a bandpass centered on a 36 bar period.
The problem seems to be on the following line...
Code:
var a = (0.707*2*PI)/CutoffPeriod;
which would be better if it said...
Code:
var a = (1.414*2*PI)/CutoffPeriod;
Here is the python script that I use to plot the transfer responses.
Click to reveal..
Save it as script.py and call it as follows. You will need recent versions of scipy, matplotlib and numpy installed.
To plot the transfer response of the built-in highpass2 filter with cutoff = 10...
Code:
python script.py hp2 10
To plot the transfer response of the built-in roof filter with cutoffs 20 & 10...
Code:
python script.py roof 20 10
To plot both in one go...
Code:
python script.py hp2 10 roof 20 10
Add as many filter names and parameters as desired. For example...
Code:
python plot_filters_zorro.py hp2 10 hp2 20 hp2 30 smooth 10 roof 24 18
Included filters...
- ema - exponential moving average
- sma - simple moving average
- hp2 - built-in highpass2
- corrected_hp2 - my proposed correction
- butterworth_hp2 - a 2-pole butterworth highpass filter
- smooth - the built-in supersmoother
- roof - the built-in roofing filter
- corrected_roof - roof with the corrected highpass filter
- butterworth_roof - roof with the 2-pole butterworth highpass filter