Skip to content

Instantly share code, notes, and snippets.

@sam81
Created October 15, 2015 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam81/dac11a42301cbab9628c to your computer and use it in GitHub Desktop.
Save sam81/dac11a42301cbab9628c to your computer and use it in GitHub Desktop.
using DSP, Base.Test
include("fir2_filter.jl")
# `freq` and `gain` have different lengths.
@test_throws ErrorException firwin2(50, [0, 0.5, 1], [0.0, 1.0])
## # `nfreqs` is less than `ntaps`.
@test_throws ErrorException firwin2(50, [0, 0.5, 1], [0.0, 1.0, 1.0], nfreqs=33)
## # Decreasing value in `freq`
@test_throws ErrorException firwin2(50, [0, 0.5, 0.4, 1.0], [0, .25, .5, 1.0])
## # Value in `freq` repeated more than once.
@test_throws ErrorException firwin2(50, [0, .1, .1, .1, 1.0],
[0.0, 0.5, 0.75, 1.0, 1.0])
## # `freq` does not start at 0.0.
@test_throws ErrorException firwin2(50, [0.5, 1.0], [0.0, 1.0])
## # Type II filter, but the gain at nyquist rate is not zero.
@test_throws ErrorException firwin2(16, [0.0, 0.5, 1.0], [0.0, 1.0, 1.0])
## # Type III filter, but the gains at nyquist and zero rate are not zero.
@test_throws ErrorException firwin2(17, [0.0, 0.5, 1.0], [0.0, 1.0, 1.0],
antisymmetric=true)
@test_throws ErrorException firwin2(17, [0.0, 0.5, 1.0], [1.0, 1.0, 0.0],
antisymmetric=true)
@test_throws ErrorException firwin2(17, [0.0, 0.5, 1.0], [1.0, 1.0, 1.0],
antisymmetric=true)
## # Type VI filter, but the gain at zero rate is not zero.
@test_throws ErrorException firwin2(16, [0.0, 0.5, 1.0], [1.0, 1.0, 0.0],
antisymmetric=true)
## ## test01
## width = 0.04
## beta = 12.0
## ntaps = 400
## # Filter is 1 from w=0 to w=0.5, then decreases linearly from 1 to 0 as w
## # increases from w=0.5 to w=1 (w=1 is the Nyquist frequency).
## freq = [0.0, 0.5, 1.0]
## gain = [1.0, 1.0, 0.0]
## taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
## freq_samples = np.array([0.0, 0.25, 0.5-width/2, 0.5+width/2,
## 0.75, 1.0-width/2])
## freqs, response = freqz(taps, worN=np.pi*freq_samples)
## assert_array_almost_equal(np.abs(response),
## [1.0, 1.0, 1.0, 1.0-width, 0.5, width], decimal=5)
## def test02(self):
## width = 0.04
## beta = 12.0
## # ntaps must be odd for positive gain at Nyquist.
## ntaps = 401
## # An ideal highpass filter.
## freq = [0.0, 0.5, 0.5, 1.0]
## gain = [0.0, 0.0, 1.0, 1.0]
## taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
## freq_samples = np.array([0.0, 0.25, 0.5-width, 0.5+width, 0.75, 1.0])
## freqs, response = freqz(taps, worN=np.pi*freq_samples)
## assert_array_almost_equal(np.abs(response),
## [0.0, 0.0, 0.0, 1.0, 1.0, 1.0], decimal=5)
## def test03(self):
## width = 0.02
## ntaps, beta = kaiserord(120, width)
## # ntaps must be odd for positive gain at Nyquist.
## ntaps = int(ntaps) | 1
## freq = [0.0, 0.4, 0.4, 0.5, 0.5, 1.0]
## gain = [1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
## taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
## freq_samples = np.array([0.0, 0.4-width, 0.4+width, 0.45,
## 0.5-width, 0.5+width, 0.75, 1.0])
## freqs, response = freqz(taps, worN=np.pi*freq_samples)
## assert_array_almost_equal(np.abs(response),
## [1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0], decimal=5)
##test04
## """Test firwin2 when window=None."""
ntaps = 5
# Ideal lowpass: gain is 1 on [0,0.5], and 0 on [0.5, 1.0]
freq = [0.0, 0.5, 0.5, 1.0]
gain = [1.0, 1.0, 0.0, 0.0]
taps = firwin2(ntaps, freq, gain, window=rect, nfreqs=8193)
alpha = 0.5 * (ntaps - 1)
m = collect(0:ntaps-1) - alpha
h = 0.5 * sinc(0.5 * m)
@test_approx_eq_eps h taps 5.0e-7 #same eps as default numpy
## test05
## """Test firwin2 for calculating Type IV filters"""
ntaps = 1500
freq = [0.0, 1.0]
gain = [0.0, 1.0]
taps = firwin2(ntaps, freq, gain, window=rect, antisymmetric=true)
@test_approx_eq_eps taps[1:floor(Int, ntaps/2)] flipud(-taps[floor(Int, ntaps/2)+1:end]) 5.0e-7 #same eps as default numpy
## freqs, response = freqz(taps, worN=2048)
## assert_array_almost_equal(abs(response), freqs / np.pi, decimal=4)
## test06
## """Test firwin2 for calculating Type III filters"""
ntaps = 1501
freq = [0.0, 0.5, 0.55, 1.0]
gain = [0.0, 0.5, 0.0, 0.0]
taps = firwin2(ntaps, freq, gain, window=rect, antisymmetric=true)
## assert_array_almost_equal(taps[: ntaps // 2], -taps[ntaps // 2 + 1:][::-1])
@test taps[floor(Int, ntaps/2)+1] == 0.0
#freqs, response1 = freqz(taps, worN=2048)
#response2 = np.interp(freqs / np.pi, freq, gain)
#assert_array_almost_equal(abs(response1), response2, decimal=3)
## test_nyq
taps1 = firwin2(80, [0.0, 0.5, 1.0], [1.0, 1.0, 0.0])
taps2 = firwin2(80, [0.0, 30.0, 60.0], [1.0, 1.0, 0.0], nyq=60.0)
@test_approx_eq_eps taps1 taps2 5.0e-7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment