summaryrefslogtreecommitdiff
path: root/src/dft.c
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-14 16:31:44 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-14 16:31:44 -0700
commitf10afce9821a81c8dd138b84a4f1ab56116af20b (patch)
tree0233ed16ced2a3e36793ff48a2ff5384dcacf74f /src/dft.c
parentd3e8a2361f78ef505c8cae2e30faefbfae5c88e1 (diff)
Add log messages, can be disabled. Made return values more consistent
Diffstat (limited to 'src/dft.c')
-rw-r--r--src/dft.c54
1 files changed, 27 insertions, 27 deletions
diff --git a/src/dft.c b/src/dft.c
index 7538b95..96630d8 100644
--- a/src/dft.c
+++ b/src/dft.c
@@ -8,43 +8,43 @@
dft_data_t discrete_fourier_transform(smrt_arena_t *arena, f64 *samples, u64 sample_count, u64 sample_rate) {
u64 freq_count = (sample_count / 2) + 1;
- f64 freq_step = (f64)sample_rate / (f64)sample_count;
+ f64 freq_step = (f64)sample_rate / (f64)sample_count;
- dft_data_t d = {.freq_count = freq_count};
- d.frequencies = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
- d.amplitudes = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
- d.phases = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
+ dft_data_t d = {.freq_count = freq_count};
+ d.frequencies = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
+ d.amplitudes = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
+ d.phases = SMRTA_ALLOC_ARRAY(arena, f64, freq_count);
- for (u64 freq_index = 0; freq_index < freq_count; freq_index++) {
- smrta_temp_t scratch = smrta_scratch_start(NULL, 0);
+ for (u64 freq_index = 0; freq_index < freq_count; freq_index++) {
+ smrta_temp_t scratch = smrta_scratch_start(NULL, 0);
- vec2d_soa_t vs;
- vs.xs = SMRTA_ALLOC_ARRAY(scratch.arena, f64, sample_count);
- vs.ys = SMRTA_ALLOC_ARRAY(scratch.arena, f64, sample_count);
- vs.size = sample_count;
+ vec2d_soa_t vs;
+ vs.xs = SMRTA_ALLOC_ARRAY(scratch.arena, f64, sample_count);
+ vs.ys = SMRTA_ALLOC_ARRAY(scratch.arena, f64, sample_count);
+ vs.size = sample_count;
- if (!vs.xs || !vs.ys) { smrta_scratch_end(scratch); return (dft_data_t){ 0 }; }
+ if (!vs.xs || !vs.ys) { smrta_scratch_end(scratch); return (dft_data_t){ 0 }; }
- for (u64 i = 0; i < sample_count; i++) {
- f64 angle = i / (f64)sample_count * PI * 2.0 * freq_index;
+ for (u64 i = 0; i < sample_count; i++) {
+ f64 angle = i / (f64)sample_count * PI * 2.0 * freq_index;
- vs.xs[i] = cos(angle) * samples[i];
- vs.ys[i] = sin(angle) * samples[i];
- }
+ vs.xs[i] = cos(angle) * samples[i];
+ vs.ys[i] = sin(angle) * samples[i];
+ }
- vec2d_t average_pos = vec2d_soa_average(&vs);
- smrta_scratch_end(scratch);
+ vec2d_t average_pos = vec2d_soa_average(&vs);
+ smrta_scratch_end(scratch);
- b8 is_zero_hz = freq_index == 0;
- b8 is_nyquist = freq_index == freq_count - 1 && sample_count % 2 == 0;
- f64 amp_coeff = is_zero_hz || is_nyquist ? 1.0 : 2.0;
+ b8 is_zero_hz = freq_index == 0;
+ b8 is_nyquist = freq_index == freq_count - 1 && sample_count % 2 == 0;
+ f64 amp_coeff = is_zero_hz || is_nyquist ? 1.0 : 2.0;
- d.frequencies[freq_index] = freq_index * freq_step;
- d.amplitudes[freq_index] = vec2d_length(average_pos) * amp_coeff;
- d.phases[freq_index] = -atan2(average_pos.y, average_pos.x);
+ d.frequencies[freq_index] = freq_index * freq_step;
+ d.amplitudes[freq_index] = vec2d_length(average_pos) * amp_coeff;
+ d.phases[freq_index] = -atan2(average_pos.y, average_pos.x);
- }
- return d;
+ }
+ return d;
}
static inline f64 hann_window(u64 i, u64 n) {