-
Notifications
You must be signed in to change notification settings - Fork 93
/
WaveletScalogram.java
224 lines (188 loc) · 8.13 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
package io.fair_acc.sample.math;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jtransforms.fft.DoubleFFT_1D;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.fair_acc.chartfx.renderer.spi.ContourDataSetRenderer;
import io.fair_acc.chartfx.renderer.spi.utils.ColorGradient;
import io.fair_acc.chartfx.utils.AxisSynchronizer;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.GridDataSet;
import io.fair_acc.dataset.spi.DefaultDataSet;
import io.fair_acc.math.Math;
import io.fair_acc.math.spectra.SpectrumTools;
import io.fair_acc.math.spectra.wavelet.ContinuousWavelet;
import io.fair_acc.sample.chart.ChartSample;
import io.fair_acc.sample.math.utils.DemoChart;
/**
* example illustrating wavelet-based scalograms
*
* @author rstein
*/
public class WaveletScalogram extends ChartSample {
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 GridDataSet fdataset;
private DefaultDataSet fwavelet;
private DefaultDataSet ffourier;
private double[] yValues;
private DataSet 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(() -> 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.getShape(DataSet.DIM_X) / 20;
for (int i = 0; i < fdataset.getShape(DataSet.DIM_Y); i++) {
double val = 0.0;
int count = 0;
for (int j = nboundary; j < fdataset.getShape(DataSet.DIM_X) - nboundary; j++) {
val += fdataset.get(DataSet.DIM_Z, i * fdataset.getShape(DataSet.DIM_X) + j);
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 = Math.maximum(magWavelet);
for (int i = 0; i < magWavelet.length; i++) {
magWavelet[i] -= maxWavelet;
}
final double maxFourier = Math.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);
fdataset.recomputeLimits(DataSet.DIM_Y);
return fdataset;
}
@Override
public Node getChartPanel(Stage stage) {
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(chart1.getYAxis());
sync.add(chart2.getXAxis());
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(Math.TWO_PI * 2e-4 * x * (x + offset)) : 0;
// single tone at 0.25
yModel[i] += (i > 50 && i < 500) ? Math.sin(Math.TWO_PI * 0.25 * x) : 0;
// modulation around 0.4
final double mod = Math.cos(Math.TWO_PI * 0.01 * x);
yModel[i] += (i > 300 && i < 900) ? Math.sin(Math.TWO_PI * (0.4 - 5e-4 * mod) * x) : 0;
// quadratic chirp starting at 0.1
yModel[i] += 0.5 * Math.sin(Math.TWO_PI * ((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(Objects.requireNonNull(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);
}
}