forked from fair-acc/chart-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveletScalogram.java
226 lines (190 loc) · 8.03 KB
/
WaveletScalogram.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package de.gsi.math.samples;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import org.jtransforms.fft.DoubleFFT_1D;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.gsi.chart.renderer.spi.ContourDataSetRenderer;
import de.gsi.chart.renderer.spi.utils.ColorGradient;
import de.gsi.chart.utils.AxisSynchronizer;
import de.gsi.dataset.DataSet;
import de.gsi.dataset.DataSet3D;
import de.gsi.dataset.spi.DefaultDataSet;
import de.gsi.math.TMath;
import de.gsi.math.samples.utils.AbstractDemoApplication;
import de.gsi.math.samples.utils.DemoChart;
import de.gsi.math.spectra.SpectrumTools;
import de.gsi.math.spectra.wavelet.ContinuousWavelet;
/**
* example illustrating wavelet-based scalograms
*
* @author rstein
*/
public class WaveletScalogram extends AbstractDemoApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(WaveletScalogram.class);
private static final int MAX_POINTS = 1024;
public static final boolean LOAD_EXAMPLE_DATA = true;
private DataSet3D fdataset;
private DefaultDataSet fwavelet;
private DefaultDataSet ffourier;
private double[] yValues;
private DataSet3D createDataSet() {
final double nu = 2 * 25;
final int nQuantx = 512;
final int nQuanty = 1024;
final double fmin = 0.05;
final double fmax = 0.50;
if (LOAD_EXAMPLE_DATA) {
// show-room data
// case 1: chirped CPS tune acquisition, the horizontal, cross-term
// tune,
// and a reference tone above 0.45 are visible
// case 2: LHC B2 horizontal injection oscillations,
// recommendation to choose nu >= 25
// -> injection synchrotron oscillations are visible
yValues = readDemoData(1);
} else {
// synthetic data
yValues = loadSyntheticData();
}
// the wavelet scalogram computation
final ContinuousWavelet wtrafo = new ContinuousWavelet();
new Thread() {
@Override
public void run() {
fdataset = wtrafo.getScalogram(yValues, nQuantx, nQuanty, nu, fmin, fmax);
}
}.start();
do {
sleep(1000);
final int status = wtrafo.getStatus();
if (status > 10) {
LOGGER.atInfo().log(status + " % of computation done");
}
} while (wtrafo.isBusy());
sleep(1000);
final DoubleFFT_1D fft = new DoubleFFT_1D(yValues.length);
final double[] fftSpectra = Arrays.copyOf(yValues, yValues.length);
fft.realForward(fftSpectra);
final double[] frequency1 = wtrafo.getScalogramFrequencyAxis(nQuantx, nQuanty, nu, fmin, fmax);
final double[] magWavelet = new double[frequency1.length];
final int nboundary = fdataset.getDataCount(DataSet.DIM_X) / 20;
for (int i = 0; i < fdataset.getDataCount(DataSet.DIM_Y); i++) {
double val = 0.0;
int count = 0;
for (int j = nboundary; j < fdataset.getDataCount(DataSet.DIM_X) - nboundary; j++) {
val += fdataset.getZ(j, i);
count++;
}
if (count > 0) {
magWavelet[i] = val / count;
}
}
final double[] magFourier = SpectrumTools.computeMagnitudeSpectrum_dB(fftSpectra, true);
final double[] frequency2 = SpectrumTools.computeFrequencyScale(fftSpectra.length / 2);
// normalise FFT and wavelet spectra for better comparison
final double maxWavelet = TMath.Maximum(magWavelet);
for (int i = 0; i < magWavelet.length; i++) {
magWavelet[i] -= maxWavelet;
}
final double maxFourier = TMath.Maximum(magFourier);
for (int i = 0; i < magFourier.length; i++) {
magFourier[i] -= maxFourier;
}
fwavelet = new DefaultDataSet("Wavelet magnitude", frequency1, magWavelet, frequency1.length, true);
ffourier = new DefaultDataSet("Fourier magnitude", frequency2, magFourier, frequency2.length, true);
return fdataset;
}
@Override
public Node getContent() {
final DemoChart chart1 = new DemoChart();
chart1.getXAxis().setName("time");
chart1.getXAxis().setUnit("turns");
chart1.getYAxis().setAutoRangeRounding(false);
chart1.getYAxis().setAutoRangePadding(0.0);
chart1.getYAxis().setName("frequency");
chart1.getYAxis().setUnit("fs");
final ContourDataSetRenderer contourChartRenderer = new ContourDataSetRenderer();
chart1.getRenderers().set(0, contourChartRenderer);
contourChartRenderer.setColorGradient(ColorGradient.RAINBOW);
// contourChartRenderer.setColorGradient(ColorGradient.JET);
// contourChartRenderer.setColorGradient(ColorGradient.TOPO_EXT);
contourChartRenderer.getDatasets().add(createDataSet());
final DemoChart chart2 = new DemoChart();
chart2.getXAxis().setName("frequency");
chart2.getXAxis().setUnit("fs");
chart2.getYAxis().setName("magnitude");
chart1.getXAxis().setAutoRangeRounding(false);
chart1.getXAxis().setAutoRangePadding(0.0);
chart2.getDatasets().addAll(fwavelet, ffourier);
final AxisSynchronizer sync = new AxisSynchronizer();
sync.add(chart2.getXAxis());
sync.add(chart1.getYAxis());
return new VBox(chart1, chart2);
}
private double[] loadSyntheticData() {
// synthetic data
final double[] yModel = new double[MAX_POINTS];
final Random rnd = new Random();
for (int i = 0; i < yModel.length; i++) {
final double x = i;
double offset = 0;
final double error = 0.1 * rnd.nextGaussian();
// linear chirp with discontinuity
offset = (i > 500) ? -20 : 0;
yModel[i] = (i > 100 && i < 700) ? 0.7 * Math.sin(TMath.TwoPi() * 2e-4 * x * (x + offset)) : 0;
// single tone at 0.25
yModel[i] += (i > 50 && i < 500) ? 1.0 * Math.sin(TMath.TwoPi() * 0.25 * x) : 0;
// modulation around 0.4
final double mod = Math.cos(TMath.TwoPi() * 0.01 * x);
yModel[i] += (i > 300 && i < 900) ? 1.0 * Math.sin(TMath.TwoPi() * (0.4 - 5e-4 * mod) * x) : 0;
// quadratic chirp starting at 0.1
yModel[i] += 0.5 * Math.sin(TMath.TwoPi() * ((0.1 + 5e-8 * x * x) * x));
yModel[i] = yModel[i] + error;
}
return yModel;
}
private double[] readDemoData(int index) {
final String fileName = index <= 1 ? "./rawDataCPS2.dat" : "./rawDataLHCInj.dat";
try {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(EMDSample.class.getResourceAsStream(fileName)))) {
String line = reader.readLine();
final int nDim = line == null ? 0 : Integer.parseInt(line);
double[] ret = new double[nDim];
for (int i = 0; i < nDim; i++) {
line = reader.readLine();
if (line == null) {
break;
}
final String[] x = line.split("\t");
ret[i] = Double.parseDouble(x[1]);
}
return ret;
}
} catch (Exception e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.atError().setCause(e).log("read error");
}
}
return new double[1000];
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.atError().setCause(e).log("InterruptedException");
}
Thread.currentThread().interrupt();
}
}
public static void main(final String[] args) {
Application.launch(args);
}
}