-
Notifications
You must be signed in to change notification settings - Fork 89
/
FrequencyFilterSample.java
80 lines (66 loc) · 3.82 KB
/
FrequencyFilterSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package io.fair_acc.sample.math;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import io.fair_acc.dataset.spi.DefaultDataSet;
import io.fair_acc.math.DataSetMath;
import io.fair_acc.math.filter.FilterType;
import io.fair_acc.math.filter.fir.FirFilter;
import io.fair_acc.sample.chart.ChartSample;
import io.fair_acc.sample.math.utils.DemoChart;
/**
* Sample to illustrate array-based Butterworth and Chebychev filters
*
* @author rstein
*/
public class FrequencyFilterSample extends ChartSample {
private static final int N_SAMPLES = 8192;
private static final int N_SAMPLE_RATE = 1000;
@Override
public Node getChartPanel(Stage stage) {
// generate some random samples
final double[] xValues = new double[N_SAMPLES];
final double[] yValues = new double[N_SAMPLES];
double fs = N_SAMPLE_RATE;
double fcut = 0.1;
for (int i = 0; i < N_SAMPLES; i++) {
xValues[i] = i / fs;
// yValues[i] = i < N_SAMPLES / 2 ? 0.0 : 1.0; // step
}
yValues[N_SAMPLES / 2] = N_SAMPLES; // dirac delta
DefaultDataSet dataSet = new DefaultDataSet("dirac", xValues, yValues, xValues.length, true);
final DemoChart chartLP = new DemoChart();
chartLP.getXAxis().setName("frequency");
chartLP.getYAxis().setName("magnitude");
chartLP.getYAxis().setUnit("dB");
chartLP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(dataSet));
DefaultDataSet butterWorthA = new DefaultDataSet("Butterworth(4th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 4, FilterType.LOW_PASS, 0), xValues.length, true);
chartLP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthA));
DefaultDataSet butterWorthB = new DefaultDataSet("Butterworth(6th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 6, FilterType.LOW_PASS, 0), xValues.length, true);
chartLP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthB));
DefaultDataSet butterWorthC = new DefaultDataSet("Butterworth(8th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 8, FilterType.LOW_PASS, 0), xValues.length, true);
chartLP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthC));
final DemoChart chartHP = new DemoChart();
chartHP.getXAxis().setName("frequency");
chartHP.getYAxis().setName("magnitude");
chartHP.getYAxis().setUnit("dB");
chartHP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(dataSet));
DefaultDataSet butterWorthA2 = new DefaultDataSet("Butterworth(4th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 4, FilterType.HIGH_PASS, 0.0), xValues.length, true);
chartHP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthA2));
DefaultDataSet butterWorthB2 = new DefaultDataSet("Butterworth(6th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 6, FilterType.HIGH_PASS, 0.0), xValues.length, true);
chartHP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthB2));
DefaultDataSet butterWorthC2 = new DefaultDataSet("Butterworth(8th)", xValues,
FirFilter.filterSignal(yValues, null, fcut, 8, FilterType.HIGH_PASS, 0.0), xValues.length, true);
chartHP.getRenderer(0).getDatasets().addAll(DataSetMath.normalisedMagnitudeSpectrumDecibel(butterWorthC2));
return new VBox(chartLP, chartHP);
}
public static void main(final String[] args) {
Application.launch(args);
}
}