use double precision where it won't affect speed

This commit is contained in:
Ahmet Inan 2024-04-24 12:03:44 +02:00
parent a9fdf1eeff
commit 02ecb77b5c
4 changed files with 27 additions and 26 deletions

View file

@ -34,9 +34,9 @@ public class Demodulator {
public float frequencyOffset; public float frequencyOffset;
Demodulator(int sampleRate) { Demodulator(int sampleRate) {
float blackFrequency = 1500; double blackFrequency = 1500;
float whiteFrequency = 2300; double whiteFrequency = 2300;
float scanLineBandwidth = whiteFrequency - blackFrequency; double scanLineBandwidth = whiteFrequency - blackFrequency;
frequencyModulation = new FrequencyModulation(scanLineBandwidth, sampleRate); frequencyModulation = new FrequencyModulation(scanLineBandwidth, sampleRate);
double syncPulse5msSeconds = 0.005; double syncPulse5msSeconds = 0.005;
double syncPulse9msSeconds = 0.009; double syncPulse9msSeconds = 0.009;
@ -54,26 +54,26 @@ public class Demodulator {
syncPulseFilterDelay = (syncPulseFilterSamples - 1) / 2; syncPulseFilterDelay = (syncPulseFilterSamples - 1) / 2;
syncPulseFilter = new SimpleMovingAverage(syncPulseFilterSamples); syncPulseFilter = new SimpleMovingAverage(syncPulseFilterSamples);
syncPulseValueDelay = new Delay(syncPulseFilterSamples); syncPulseValueDelay = new Delay(syncPulseFilterSamples);
float lowestFrequency = 1000; double lowestFrequency = 1000;
float highestFrequency = 2800; double highestFrequency = 2800;
float cutoffFrequency = (highestFrequency - lowestFrequency) / 2; double cutoffFrequency = (highestFrequency - lowestFrequency) / 2;
double baseBandLowPassSeconds = 0.002; double baseBandLowPassSeconds = 0.002;
int baseBandLowPassSamples = (int) Math.round(baseBandLowPassSeconds * sampleRate) | 1; int baseBandLowPassSamples = (int) Math.round(baseBandLowPassSeconds * sampleRate) | 1;
baseBandLowPass = new ComplexConvolution(baseBandLowPassSamples); baseBandLowPass = new ComplexConvolution(baseBandLowPassSamples);
Kaiser kaiser = new Kaiser(); Kaiser kaiser = new Kaiser();
for (int i = 0; i < baseBandLowPass.length; ++i) for (int i = 0; i < baseBandLowPass.length; ++i)
baseBandLowPass.taps[i] = (float) (kaiser.window(2.0, i, baseBandLowPass.length) * Filter.lowPass(cutoffFrequency, sampleRate, i, baseBandLowPass.length)); baseBandLowPass.taps[i] = (float) (kaiser.window(2.0, i, baseBandLowPass.length) * Filter.lowPass(cutoffFrequency, sampleRate, i, baseBandLowPass.length));
float centerFrequency = (lowestFrequency + highestFrequency) / 2; double centerFrequency = (lowestFrequency + highestFrequency) / 2;
baseBandOscillator = new Phasor(-centerFrequency, sampleRate); baseBandOscillator = new Phasor(-centerFrequency, sampleRate);
float syncPulseFrequency = 1200; double syncPulseFrequency = 1200;
syncPulseFrequencyValue = (syncPulseFrequency - centerFrequency) * 2 / scanLineBandwidth; syncPulseFrequencyValue = (float) ((syncPulseFrequency - centerFrequency) * 2 / scanLineBandwidth);
syncPulseFrequencyTolerance = 50 * 2 / scanLineBandwidth; syncPulseFrequencyTolerance = (float) (50 * 2 / scanLineBandwidth);
float syncPorchFrequency = 1500; double syncPorchFrequency = 1500;
float syncHighFrequency = (syncPulseFrequency + syncPorchFrequency) / 2; double syncHighFrequency = (syncPulseFrequency + syncPorchFrequency) / 2;
float syncLowFrequency = (syncPulseFrequency + syncHighFrequency) / 2; double syncLowFrequency = (syncPulseFrequency + syncHighFrequency) / 2;
float syncLowValue = (syncLowFrequency - centerFrequency) * 2 / scanLineBandwidth; double syncLowValue = (syncLowFrequency - centerFrequency) * 2 / scanLineBandwidth;
float syncHighValue = (syncHighFrequency - centerFrequency) * 2 / scanLineBandwidth; double syncHighValue = (syncHighFrequency - centerFrequency) * 2 / scanLineBandwidth;
syncPulseTrigger = new SchmittTrigger(syncLowValue, syncHighValue); syncPulseTrigger = new SchmittTrigger((float) syncLowValue, (float) syncHighValue);
baseBand = new Complex(); baseBand = new Complex();
} }

View file

@ -6,6 +6,7 @@ Copyright 2024 Ahmet Inan <xdsopl@gmail.com>
package xdsopl.robot36; package xdsopl.robot36;
@SuppressWarnings("unused")
public class ExponentialMovingAverage { public class ExponentialMovingAverage {
private float alpha; private float alpha;
private float prev; private float prev;
@ -18,20 +19,20 @@ public class ExponentialMovingAverage {
return prev = prev * (1 - alpha) + alpha * input; return prev = prev * (1 - alpha) + alpha * input;
} }
public void alpha(float alpha) { public void alpha(double alpha) {
this.alpha = alpha; this.alpha = (float) alpha;
} }
public void alpha(float alpha, int order) { public void alpha(double alpha, int order) {
alpha((float) Math.pow(alpha, 1.0 / order)); alpha(Math.pow(alpha, 1.0 / order));
} }
public void cutoff(float freq, float rate, int order) { public void cutoff(double freq, double rate, int order) {
double x = Math.cos(2 * Math.PI * freq / rate); double x = Math.cos(2 * Math.PI * freq / rate);
alpha((float) (x-1+Math.sqrt(x*(x-4)+3)), order); alpha(x - 1 + Math.sqrt(x * (x - 4) + 3), order);
} }
public void cutoff(float freq, float rate) { public void cutoff(double freq, double rate) {
cutoff(freq, rate, 1); cutoff(freq, rate, 1);
} }

View file

@ -10,10 +10,10 @@ public class FrequencyModulation {
private float prev; private float prev;
private final float scale; private final float scale;
private final float Pi, TwoPi; private final float Pi, TwoPi;
FrequencyModulation(float bandwidth, float sampleRate) { FrequencyModulation(double bandwidth, double sampleRate) {
this.Pi = (float) Math.PI; this.Pi = (float) Math.PI;
this.TwoPi = 2 * this.Pi; this.TwoPi = 2 * this.Pi;
scale = sampleRate / (bandwidth * Pi); this.scale = (float) (sampleRate / (bandwidth * Math.PI));
} }
private float wrap(float value) { private float wrap(float value) {
if (value < -Pi) if (value < -Pi)

View file

@ -9,7 +9,7 @@ package xdsopl.robot36;
public class Phasor { public class Phasor {
private final Complex value; private final Complex value;
private final Complex delta; private final Complex delta;
Phasor(float freq, float rate) { Phasor(double freq, double rate) {
value = new Complex(1, 0); value = new Complex(1, 0);
double omega = 2 * Math.PI * freq / rate; double omega = 2 * Math.PI * freq / rate;
delta = new Complex((float) Math.cos(omega), (float) Math.sin(omega)); delta = new Complex((float) Math.cos(omega), (float) Math.sin(omega));