You have to try to measure the loop'ss duration inside the DHO804

Do we have python/numpy which fits inside the android there??
Or simply redo it into C and do measure..
Below the Bessel I0=1 in the Kaiser it seems..
How to measure magnitude ratio in decibels and phase in degrees of two sine waves with help of Kaiser window and something like DFT at given frequency, while not using complex exponential function and not using costly sin and cos and atan2 functions? Do use atan2 approximation. Could you show me a C code?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Constants
#define PI 3.14159265358979323846
// Kaiser Window function
void compute_kaiser_window(double* window, int length, double beta) {
for (int n = 0; n < length; n++) {
double term = (2.0 * n / (length - 1)) - 1.0;
window[n] = exp(-beta * (1 - term * term));
}
}
// Apply Kaiser window to a signal
void apply_window(double* signal, const double* window, int length) {
for (int i = 0; i < length; i++) {
signal[i] *= window[i];
}
}
// Recursive trigonometric DFT computation
void compute_dft_at_frequency(const double* signal, int length, double freq, double sampling_rate, double* real_out, double* imag_out) {
double omega = 2 * PI * freq / sampling_rate;
double cos_omega = cos(omega);
double sin_omega = sin(omega);
double real = 0.0;
double imag = 0.0;
// Initialize recursive variables
double cos_prev = 1.0;
double sin_prev = 0.0;
double cos_curr, sin_curr;
for (int n = 0; n < length; n++) {
real += signal[n] * cos_prev;
imag -= signal[n] * sin_prev; // Negative for DFT
// Recursive update of cos and sin
cos_curr = cos_omega * cos_prev - sin_omega * sin_prev;
sin_curr = sin_omega * cos_prev + cos_omega * sin_prev;
cos_prev = cos_curr;
sin_prev = sin_curr;
}
*real_out = real;
*imag_out = imag;
}
// Approximate atan2
double approximate_atan2(double y, double x) {
if (x == 0.0) {
return (y > 0.0 ? PI / 2 : (y < 0.0 ? -PI / 2 : 0.0));
}
double abs_x = fabs(x);
double abs_y = fabs(y);
double ratio = y / (abs_x + sqrt(x * x + y * y));
double angle = (PI / 4) * ratio;
if (x > 0.0) return angle;
if (y >= 0.0) return angle + PI;
return angle - PI;
}
// Calculate magnitude and phase
void calculate_magnitude_and_phase(double real_x, double imag_x, double real_y, double imag_y, double* magnitude_db, double* phase_deg) {
// Magnitudes
double mag_x = sqrt(real_x * real_x + imag_x * imag_x);
double mag_y = sqrt(real_y * real_y + imag_y * imag_y);
// Magnitude ratio in dB
*magnitude_db = 20 * log10(mag_y / mag_x);
// Phase difference using atan2 approximation
double phase_x = approximate_atan2(imag_x, real_x);
double phase_y = approximate_atan2(imag_y, real_y);
*phase_deg = (phase_y - phase_x) * 180.0 / PI;
}
// Main function
int main() {
int length = 1024;
double sampling_rate = 1000.0; // Hz
double target_freq = 50.0; // Hz
double beta = 5.0; // Kaiser beta
// Signals
double x[1024];
double y[1024];
double window[1024];
// Generate test signals
for (int n = 0; n < length; n++) {
x[n] = sin(2 * PI * target_freq * n / sampling_rate);
y[n] = 0.5 * sin(2 * PI * target_freq * n / sampling_rate + PI / 4);
}
// Compute Kaiser window
compute_kaiser_window(window, length, beta);
// Apply window to signals
apply_window(x, window, length);
apply_window(y, window, length);
// Compute DFT at target frequency
double real_x, imag_x, real_y, imag_y;
compute_dft_at_frequency(x, length, target_freq, sampling_rate, &real_x, &imag_x);
compute_dft_at_frequency(y, length, target_freq, sampling_rate, &real_y, &imag_y);
// Calculate magnitude and phase
double magnitude_db = 0.0;
double phase_deg = 0.0;
calculate_magnitude_and_phase(real_x, imag_x, real_y, imag_y, &magnitude_db, &phase_deg);
// Print results
printf("Magnitude Ratio (dB): %f\n", magnitude_db);
printf("Phase Difference (degrees): %f\n", phase_deg);
return 0;
}
Also
#include <stdio.h>
#include <math.h>
// Function to compute the modified Bessel function I0(x)
double bessel_I0(double x) {
double sum = 1.0; // Initialize sum for the series
double term = 1.0; // First term in the series
double k = 1.0; // Iteration counter
double x2 = (x / 2.0) * (x / 2.0); // (x/2)^2 for repeated use
while (term > 1e-10) { // Converge until the term is very small
term *= x2 / (k * k);
sum += term;
k += 1.0;
}
return sum;
}
// Function to compute Kaiser window coefficients
void compute_kaiser_window(double* window, int length, double beta) {
double denom = bessel_I0(beta); // Denominator I0(beta)
for (int n = 0; n < length; n++) {
double ratio = (2.0 * n / (length - 1)) - 1.0; // Calculate (2n/(N-1) - 1)
double term = beta * sqrt(1.0 - ratio * ratio); // beta * sqrt(1 - ratio^2)
window[n] = bessel_I0(term) / denom; // w[n] = I0(term) / I0(beta)
}
}
// Test the Kaiser window computation
int main() {
int length = 16; // Length of the window
double beta = 5.0; // Shape parameter
double window[16];
// Compute the Kaiser window
compute_kaiser_window(window, length, beta);
// Print the coefficients
printf("Kaiser Window Coefficients:\n");
for (int n = 0; n < length; n++) {
printf("w[%d] = %f\n", n, window[n]);
}
return 0;
}