Fix synchronization problem in RawDataTransfroms

This commit is contained in:
Douglas Gillespie 2022-09-13 09:07:11 +01:00
parent 6b15b3d536
commit cd89c65d7c

View File

@ -188,29 +188,31 @@ public class RawDataTransforms {
* @param fftLength * @param fftLength
* @return Power spectrum * @return Power spectrum
*/ */
public synchronized double[] getPowerSpectrum(int channel, int fftLength) { public double[] getPowerSpectrum(int channel, int fftLength) {
if (powerSpectra == null) { synchronized (synchObject) {
powerSpectra = new double[PamUtils.getNumChannels(dataUnit.getChannelBitmap())][]; if (powerSpectra == null) {
} powerSpectra = new double[PamUtils.getNumChannels(dataUnit.getChannelBitmap())][];
if (fftLength == 0) {
fftLength = getCurrentSpectrumLength();
}
if (powerSpectra[channel] == null
|| powerSpectra[channel].length != fftLength / 2) {
ComplexArray cData = getComplexSpectrumHann(channel, fftLength);
currentSpecLen = fftLength;
powerSpectra[channel] = cData.magsq();
if (powerSpectra==null){
System.err.println("DLDetection: could not calculate power spectra");
return null;
} }
if (powerSpectra[channel].length != fftLength/2) { if (fftLength == 0) {
powerSpectra[channel] = Arrays.copyOf(powerSpectra[channel], fftLength/2); fftLength = getCurrentSpectrumLength();
} }
if (powerSpectra[channel] == null
|| powerSpectra[channel].length != fftLength / 2) {
ComplexArray cData = getComplexSpectrumHann(channel, fftLength);
currentSpecLen = fftLength;
powerSpectra[channel] = cData.magsq();
if (powerSpectra==null){
System.err.println("DLDetection: could not calculate power spectra");
return null;
}
if (powerSpectra[channel].length != fftLength/2) {
powerSpectra[channel] = Arrays.copyOf(powerSpectra[channel], fftLength/2);
}
}
return powerSpectra[channel];
} }
return powerSpectra[channel];
} }
@ -220,25 +222,27 @@ public class RawDataTransforms {
* @param fftLength * @param fftLength
* @return Sum of power spectra * @return Sum of power spectra
*/ */
public synchronized double[] getTotalPowerSpectrum(int fftLength) { public double[] getTotalPowerSpectrum(int fftLength) {
if (fftLength == 0) { synchronized (synchObject) {
fftLength = getCurrentSpectrumLength(); if (fftLength == 0) {
} fftLength = getCurrentSpectrumLength();
if (fftLength == 0) { }
fftLength = PamUtils.getMinFftLength(getSampleDuration()); if (fftLength == 0) {
} fftLength = PamUtils.getMinFftLength(getSampleDuration());
double[] ps; }
if (totalPowerSpectrum == null double[] ps;
|| totalPowerSpectrum.length != fftLength / 2) { if (totalPowerSpectrum == null
totalPowerSpectrum = new double[fftLength / 2]; || totalPowerSpectrum.length != fftLength / 2) {
for (int c = 0; c < PamUtils.getNumChannels(this.dataUnit.getChannelBitmap()); c++) { totalPowerSpectrum = new double[fftLength / 2];
ps = getPowerSpectrum(c, fftLength); for (int c = 0; c < PamUtils.getNumChannels(this.dataUnit.getChannelBitmap()); c++) {
for (int i = 0; i < fftLength / 2; i++) { ps = getPowerSpectrum(c, fftLength);
totalPowerSpectrum[i] += ps[i]; for (int i = 0; i < fftLength / 2; i++) {
totalPowerSpectrum[i] += ps[i];
}
} }
} }
return totalPowerSpectrum;
} }
return totalPowerSpectrum;
} }
@ -253,15 +257,17 @@ public class RawDataTransforms {
* @param fftLength - the FFT length to use. * @param fftLength - the FFT length to use.
* @return the complex spectrum - the comnplex spectrum of the wave data from the specified channel. * @return the complex spectrum - the comnplex spectrum of the wave data from the specified channel.
*/ */
public synchronized ComplexArray getComplexSpectrumHann(int channel, int fftLength) { public ComplexArray getComplexSpectrumHann(int channel, int fftLength) {
complexSpectrum = new ComplexArray[PamUtils.getNumChannels(dataUnit.getChannelBitmap())]; synchronized (synchObject) {
if (complexSpectrum[channel] == null complexSpectrum = new ComplexArray[PamUtils.getNumChannels(dataUnit.getChannelBitmap())];
|| complexSpectrum.length != fftLength / 2) { if (complexSpectrum[channel] == null
|| complexSpectrum.length != fftLength / 2) {
complexSpectrum[channel] = getComplexSpectrumHann(rawData.getWaveData()[channel], fftLength); complexSpectrum[channel] = getComplexSpectrumHann(rawData.getWaveData()[channel], fftLength);
currentSpecLen = fftLength; currentSpecLen = fftLength;
}
return complexSpectrum[channel];
} }
return complexSpectrum[channel];
} }
@ -389,29 +395,31 @@ public class RawDataTransforms {
* @param fftLength * @param fftLength
* @return the complex spectrum * @return the complex spectrum
*/ */
public synchronized ComplexArray getComplexSpectrum(int channel, int fftLength) { public ComplexArray getComplexSpectrum(int channel, int fftLength) {
double[] paddedRawData; synchronized (synchObject) {
double[] rawData; double[] paddedRawData;
int i, mn; double[] rawData;
int i, mn;
if (complexSpectrum == null) { if (complexSpectrum == null) {
complexSpectrum = new ComplexArray[getNChan()]; complexSpectrum = new ComplexArray[getNChan()];
}
if (complexSpectrum[channel] == null
|| complexSpectrum.length != fftLength / 2) {
paddedRawData = new double[fftLength];
rawData = getWaveData(channel);
//double[] rotData = getRotationCorrection(channel);
mn = Math.min(fftLength, getSampleDuration().intValue());
for (i = 0; i < mn; i++) {
paddedRawData[i] = rawData[i];//-rotData[i];
} }
for (i = mn; i < fftLength; i++) { if (complexSpectrum[channel] == null
paddedRawData[i] = 0; || complexSpectrum.length != fftLength / 2) {
paddedRawData = new double[fftLength];
rawData = getWaveData(channel);
//double[] rotData = getRotationCorrection(channel);
mn = Math.min(fftLength, getSampleDuration().intValue());
for (i = 0; i < mn; i++) {
paddedRawData[i] = rawData[i];//-rotData[i];
}
for (i = mn; i < fftLength; i++) {
paddedRawData[i] = 0;
}
complexSpectrum[channel] = fastFFT.rfft(paddedRawData, fftLength);
} }
complexSpectrum[channel] = fastFFT.rfft(paddedRawData, fftLength); return complexSpectrum[channel];
} }
return complexSpectrum[channel];
} }
@ -420,14 +428,16 @@ public class RawDataTransforms {
* @param iChan channel index * @param iChan channel index
* @return analytic waveform * @return analytic waveform
*/ */
public synchronized double[] getAnalyticWaveform(int iChan) { public double[] getAnalyticWaveform(int iChan) {
if (analyticWaveform == null) { synchronized (synchObject) {
analyticWaveform = new double[getNChan()][]; if (analyticWaveform == null) {
analyticWaveform = new double[getNChan()][];
}
// if (analyticWaveform[iChan] == null) {
analyticWaveform[iChan] = hilbert.getHilbert(getWaveData(iChan));
// }
return analyticWaveform[iChan];
} }
// if (analyticWaveform[iChan] == null) {
analyticWaveform[iChan] = hilbert.getHilbert(getWaveData(iChan));
// }
return analyticWaveform[iChan];
} }
/** /**
@ -439,12 +449,14 @@ public class RawDataTransforms {
* @param fftFilterParams fft filter parameters. * @param fftFilterParams fft filter parameters.
* @return analystic waveform. * @return analystic waveform.
*/ */
public synchronized double[] getAnalyticWaveform(int iChan, boolean filtered, FFTFilterParams fftFilterParams) { public double[] getAnalyticWaveform(int iChan, boolean filtered, FFTFilterParams fftFilterParams) {
if (filtered == false || fftFilterParams == null) { synchronized (synchObject) {
return getAnalyticWaveform(iChan); if (filtered == false || fftFilterParams == null) {
} return getAnalyticWaveform(iChan);
else { }
return getFilteredAnalyticWaveform(fftFilterParams, iChan); else {
return getFilteredAnalyticWaveform(fftFilterParams, iChan);
}
} }
} }