Skip to content

Commit

Permalink
Merge pull request #4 from Am6er/feat/sliding-spectrum-change
Browse files Browse the repository at this point in the history
feat (spectrum-change): use sliding window for spectrum change funtionality
  • Loading branch information
Am6er authored Oct 1, 2024
2 parents 6c69420 + bcf1eb6 commit 7cd1bb3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
39 changes: 30 additions & 9 deletions app/src/main/java/org/fe57/atomspectra/AtomSpectraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class AtomSpectraService extends Service {
//data for spectrum
private static final double[] histogram = new double[1024];
public static long[] histogram_all_delta = new long[Constants.NUM_HIST_POINTS]; //array to save delta
public static long[] histogram_all_prev_time = new long[Constants.NUM_HIST_POINTS]; //array to save delta
public static final LinkedList<long[]> histogram_all_queue = new LinkedList<long[]>(); //array to save delta window
private static final long[] referencePulse = new long[1024];
private static final double[] referenceDoublePulse = new double[1024];
private static final double[] realTimeX = new double[1024];
Expand Down Expand Up @@ -1341,7 +1341,9 @@ public void DeleteSpc() {
ForegroundSpectrum.initSpectrumData().setSuffix(getString(R.string.hist_suffix));
ForegroundSaveSpectrum.initSpectrumData();
Arrays.fill(histogram_all_delta, 0);
Arrays.fill(histogram_all_prev_time, 0);
synchronized (histogram_all_queue) {
histogram_all_queue.clear();
}
total_pulses = 0;
synchronized (windowCps) {
windowCps.clear();
Expand Down Expand Up @@ -1891,13 +1893,32 @@ public void run() {
}
}
deltaTimeAccumulator++;
if (deltaTimeAccumulator >= (delta_time * 1000 / Constants.UPDATE_PERIOD)) {
deltaTimeAccumulator = 0;
long[] temp = Arrays.copyOf(ForegroundSpectrum.getDataArray(), ForegroundSpectrum.getDataArray().length);
for (int i = 0; i < temp.length; i++) {
histogram_all_delta[i] = temp[i] - histogram_all_prev_time[i];
histogram_all_prev_time[i] = temp[i];
if (deltaTimeAccumulator >= (1000 / Constants.UPDATE_PERIOD)) { // each second
if (showDelta && !freeze_update_data) {
long[] currentState = Arrays.copyOf(ForegroundSpectrum.getDataArray(), ForegroundSpectrum.getDataArray().length);
long[] previousState = currentState;
synchronized (histogram_all_queue) {
histogram_all_queue.add(currentState);
while (histogram_all_queue.size() > delta_time + 1) {
histogram_all_queue.remove();
}

previousState = histogram_all_queue.peek();
}

for (int i = 0; i < currentState.length; i++) {
histogram_all_delta[i] = currentState[i] - previousState[i];
}
} else {
// cleanup queue
if (!histogram_all_queue.isEmpty()) {
synchronized (histogram_all_queue) {
histogram_all_queue.clear();
}
}
}

deltaTimeAccumulator = 0;
}
synchronized (countTimeSync) {
if (countTime < periodUpdate) {
Expand Down Expand Up @@ -2501,7 +2522,7 @@ private void autosaveHist() {

if (autosaveSpectrum == null) {
autosaveSpectrum = new Spectrum(AtomSpectraService.ForegroundSpectrum);
autosavePair = SpectrumFile.prepareOutputStream(this, sharedPreferences.getString(Constants.CONFIG.CONF_DIRECTORY_SELECTED, null), autosaveSpectrum.getSpectrumDate(), "Spectrogram" + '-' + autosaveSpectrum.getSuffix(), fileNamePrefix,"auto", ".txt", "text/plain", true, true, false);
autosavePair = SpectrumFile.prepareOutputStream(this, sharedPreferences.getString(Constants.CONFIG.CONF_DIRECTORY_SELECTED, null), autosaveSpectrum.getSpectrumDate(), "Spectrogram" + '-' + autosaveSpectrum.getSuffix(), fileNamePrefix, "auto", ".txt", "text/plain", true, true, false);

if (autosavePair == null) {
new Handler(getMainLooper()).post(() -> Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.perm_no_write_histogram), Toast.LENGTH_LONG).show());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,9 @@ public void onClick_Delta_Time_minus(View v) {
SharedPreferences settings = getSharedPreferences(Constants.ATOMSPECTRA_PREFERENCES, MODE_PRIVATE);
int r = settings.getInt(Constants.CONFIG.CONF_DELTA_TIME, Constants.DEFAULT_DELTA_TIME);

if (r > 10) {
if (r > 60) {
r = r - 30;
} else if (r > 10) {
r = r - 5;
} else if (r > 1) {
r = r - 1;
Expand All @@ -1493,8 +1495,10 @@ public void onClick_Delta_Time_plus(View v) {
r = r + 1;
} else if (r < 60) {
r = r + 5;
} else if (r < 300) {
r = r + 30;
} else {
r = 60;
r = 300;
}

SharedPreferences.Editor prefEditor = settings.edit();
Expand Down

0 comments on commit 7cd1bb3

Please sign in to comment.