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;
Demodulator(int sampleRate) {
float blackFrequency = 1500;
float whiteFrequency = 2300;
float scanLineBandwidth = whiteFrequency - blackFrequency;
double blackFrequency = 1500;
double whiteFrequency = 2300;
double scanLineBandwidth = whiteFrequency - blackFrequency;
frequencyModulation = new FrequencyModulation(scanLineBandwidth, sampleRate);
double syncPulse5msSeconds = 0.005;
double syncPulse9msSeconds = 0.009;
@ -54,26 +54,26 @@ public class Demodulator {
syncPulseFilterDelay = (syncPulseFilterSamples - 1) / 2;
syncPulseFilter = new SimpleMovingAverage(syncPulseFilterSamples);
syncPulseValueDelay = new Delay(syncPulseFilterSamples);
float lowestFrequency = 1000;
float highestFrequency = 2800;
float cutoffFrequency = (highestFrequency - lowestFrequency) / 2;
double lowestFrequency = 1000;
double highestFrequency = 2800;
double cutoffFrequency = (highestFrequency - lowestFrequency) / 2;
double baseBandLowPassSeconds = 0.002;
int baseBandLowPassSamples = (int) Math.round(baseBandLowPassSeconds * sampleRate) | 1;
baseBandLowPass = new ComplexConvolution(baseBandLowPassSamples);
Kaiser kaiser = new Kaiser();
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));
float centerFrequency = (lowestFrequency + highestFrequency) / 2;
double centerFrequency = (lowestFrequency + highestFrequency) / 2;
baseBandOscillator = new Phasor(-centerFrequency, sampleRate);
float syncPulseFrequency = 1200;
syncPulseFrequencyValue = (syncPulseFrequency - centerFrequency) * 2 / scanLineBandwidth;
syncPulseFrequencyTolerance = 50 * 2 / scanLineBandwidth;
float syncPorchFrequency = 1500;
float syncHighFrequency = (syncPulseFrequency + syncPorchFrequency) / 2;
float syncLowFrequency = (syncPulseFrequency + syncHighFrequency) / 2;
float syncLowValue = (syncLowFrequency - centerFrequency) * 2 / scanLineBandwidth;
float syncHighValue = (syncHighFrequency - centerFrequency) * 2 / scanLineBandwidth;
syncPulseTrigger = new SchmittTrigger(syncLowValue, syncHighValue);
double syncPulseFrequency = 1200;
syncPulseFrequencyValue = (float) ((syncPulseFrequency - centerFrequency) * 2 / scanLineBandwidth);
syncPulseFrequencyTolerance = (float) (50 * 2 / scanLineBandwidth);
double syncPorchFrequency = 1500;
double syncHighFrequency = (syncPulseFrequency + syncPorchFrequency) / 2;
double syncLowFrequency = (syncPulseFrequency + syncHighFrequency) / 2;
double syncLowValue = (syncLowFrequency - centerFrequency) * 2 / scanLineBandwidth;
double syncHighValue = (syncHighFrequency - centerFrequency) * 2 / scanLineBandwidth;
syncPulseTrigger = new SchmittTrigger((float) syncLowValue, (float) syncHighValue);
baseBand = new Complex();
}

View file

@ -6,6 +6,7 @@ Copyright 2024 Ahmet Inan <xdsopl@gmail.com>
package xdsopl.robot36;
@SuppressWarnings("unused")
public class ExponentialMovingAverage {
private float alpha;
private float prev;
@ -18,20 +19,20 @@ public class ExponentialMovingAverage {
return prev = prev * (1 - alpha) + alpha * input;
}
public void alpha(float alpha) {
this.alpha = alpha;
public void alpha(double alpha) {
this.alpha = (float) alpha;
}
public void alpha(float alpha, int order) {
alpha((float) Math.pow(alpha, 1.0 / order));
public void alpha(double alpha, int 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);
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);
}

View file

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

View file

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