Skip to content

Commit

Permalink
Added python script to see the frequency response.
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Jan 9, 2021
1 parent a8ba370 commit 7da31b8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 117 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ under `target/site` describing all commands in detail.
`mvn test` creates impulse responses in the subdirectories
for the different filters: `target/surefire-reports`.

To see the frequency responses install octave, copy the script
'src/test/resources/filtertest.m'
in these subdirectories and run it from there. You should see the
different frequency reponses for high/low/stop/bandpass. You can try
out different filter parameters by modifiing the test
scripts and re-run 'mvn test'.
To see the impulse and frequency responses run:
```
python3 ./plot_impulse_fresponse.py <filter>
```
where is <filter> is bessel, butterworth, chebyshevI or chebyshevII.

The script DetectorTest uses a bandpass filter to detect the
heartbeats of an ECG recording faking a matched filter which could
Expand Down
51 changes: 51 additions & 0 deletions plot_impulse_fresponse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python3

import matplotlib.pyplot as plt
import numpy as np
import sys

# Plots the impulse response of the Bandstop and its frequency response

def plot_if(figno,name,figtitle):
plt.figure(figno)
plt.suptitle(figtitle)
fs = 250
y = np.loadtxt(name);
plt.subplot(311)
plt.title("Impulse response")
plt.plot(y);
#
# Fourier Transform
yf = np.fft.fft(y)
plt.subplot(312)
fx = np.linspace(0,fs,len(yf))
plt.plot(fx,20*np.log10(abs(yf)))
plt.xlim(0,fs/2)
plt.title("Frequency response")
plt.xlabel("f/Hz")
plt.ylabel("gain/dB")

plt.subplot(313)
p = -np.diff(np.unwrap(np.angle(yf))) / np.diff(fx * 2 * np.pi)
plt.plot(np.linspace(0,fs,len(yf)-1),p)
plt.xlim(0,fs/2)
plt.ylim(-0.075,0.075)
plt.title("Phase response")
plt.xlabel("f/Hz")
plt.ylabel("delay/secs")

if len(sys.argv) < 2:
print("Specify which filter shall be plotted: bessel, butterworth, chebyshevI, chebyshevII.")
quit()

prefix = "target/surefire-reports/"+sys.argv[1]+"/"

plot_if(1,prefix+"lp.txt","Lowpass")

plot_if(2,prefix+"hp.txt","Highpass")

plot_if(3,prefix+"bs.txt","Bandstop")

plot_if(4,prefix+"bp.txt","Bandpass")

plt.show()
93 changes: 42 additions & 51 deletions src/test/java/uk/me/berndporr/iirj/ButterworthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class ButterworthTest {

static String prefix="target/surefire-reports/butterworth/";

static double fs = 250;
static double fc = 10;
static int order = 6;
static int nSteps = 10000;

void createDir() throws Exception {
File dir = new File(prefix);
dir.mkdirs();
Expand All @@ -40,129 +45,115 @@ void createDir() throws Exception {
public void lowPassTest() throws Exception {

Butterworth butterworth = new Butterworth();
butterworth.lowPass(4, 250, 50);
butterworth.lowPass(order, fs, fc);

createDir();
FileOutputStream os = new FileOutputStream(prefix+"lp.txt");
PrintStream bp = new PrintStream(os);

// let's do an impulse response
for (int i = 0; i < 500; i++) {
double v = 0;
double v = 0;
for (int i = 0; i < nSteps; i++) {
v = 0;
if (i == 10)
v = 1;
v = butterworth.filter(v);
bp.println("" + v);
}
Assert.assertTrue(Math.abs(butterworth.filter(0))<1E-80);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=0.0);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=Double.NaN);
System.out.println("Lowpass filter output = "+v);
Assert.assertTrue(v < 1E-80);
Assert.assertTrue(v != 0.0);
Assert.assertTrue(v != Double.NaN);

os.close();
}

@Test
public void bandPassTest() throws Exception {
Butterworth butterworth = new Butterworth();
butterworth.bandPass(2, 250, 50, 5);
butterworth.bandPass(order, fs, fc, fc/4);

createDir();
FileOutputStream os = new FileOutputStream(prefix+"bp.txt");
PrintStream bp = new PrintStream(os);

// let's do an impulse response
for (int i = 0; i < 500; i++) {
double v = 0;
double v = 0;
for (int i = 0; i < nSteps; i++) {
v = 0;
if (i == 10)
v = 1;
v = butterworth.filter(v);
bp.println("" + v);
}
Assert.assertTrue(Math.abs(butterworth.filter(0))<1E-10);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=0.0);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=Double.NaN);
System.out.println("Bandpass filter output = "+v);
Assert.assertTrue(v < 1E-10);
Assert.assertTrue(v != 0.0);
Assert.assertTrue(v != Double.NaN);

os.close();
}

@Test
public void bandStopTest() throws Exception {
Butterworth butterworth = new Butterworth();
butterworth.bandStop(2, 250, 50, 5);
butterworth.bandStop(order, fs, fc, fc/4);

createDir();
FileOutputStream os = new FileOutputStream(prefix+"bs.txt");
PrintStream bp = new PrintStream(os);

// let's do an impulse response
for (int i = 0; i < 500; i++) {
double v = 0;
double v = 0;
for (int i = 0; i < nSteps; i++) {
v = 0;
if (i == 10)
v = 1;
v = butterworth.filter(v);
bp.println("" + v);
}
Assert.assertTrue(Math.abs(butterworth.filter(0))<1E-10);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=0.0);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=Double.NaN);

os.close();
}

@Test
public void bandStopDCTest() throws Exception {
Butterworth butterworth = new Butterworth();
butterworth.bandStop(2, 250, 50, 5);

createDir();
FileOutputStream os = new FileOutputStream(prefix+"bsdc.txt");
PrintStream bp = new PrintStream(os);

// let's do an impulse response
for (int i = 0; i < 500; i++) {
double v = 0;
if (i>10) v = 1;
v = butterworth.filter(v);
bp.println("" + v);
}
Assert.assertTrue(Math.abs(butterworth.filter(1))>0.99999999);
Assert.assertTrue(Math.abs(butterworth.filter(1))<1.00000001);
Assert.assertTrue(Math.abs(butterworth.filter(1))!=Double.NaN);
System.out.println("Bandstop filter output = "+v);
Assert.assertTrue(v < 1E-10);
Assert.assertTrue(v != 0.0);
Assert.assertTrue(v != Double.NaN);

os.close();
}

@Test
public void highPassTest() throws Exception {
Butterworth butterworth = new Butterworth();
butterworth.highPass(4, 250, 50);
butterworth.highPass(order, fs, fc);

createDir();
FileOutputStream os = new FileOutputStream(prefix+"hp.txt");
PrintStream bp = new PrintStream(os);

// let's do an impulse response
for (int i = 0; i < 500; i++) {
double v = 0;
double v = 0;
for (int i = 0; i < nSteps; i++) {
v = 0;
if (i == 10)
v = 1;
v = butterworth.filter(v);
bp.println("" + v);
}
Assert.assertTrue(Math.abs(butterworth.filter(0))<1E-80);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=0.0);
Assert.assertTrue(Math.abs(butterworth.filter(0))!=Double.NaN);
System.out.println("Highpass filter output = "+v);
Assert.assertTrue(v < 1E-80);
Assert.assertTrue(v != 0.0);
Assert.assertTrue(v != Double.NaN);

os.close();
}

public void main(String args[]) {
try {
lowPassTest();
highPassTest();
bandPassTest();
bandStopTest();
lowPassTest();
highPassTest();
bandPassTest();
bandStopTest();
} catch (Exception e) {
Assert.fail("Exception while executing the filtering op:"+e.getMessage());
}
}
}
60 changes: 0 additions & 60 deletions src/test/resources/filtertest.m

This file was deleted.

0 comments on commit 7da31b8

Please sign in to comment.