uus fft
This commit is contained in:
88
src/fft.cpp
88
src/fft.cpp
@ -1,78 +1,28 @@
|
|||||||
#include "fft.h"
|
#include "fft.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
static float window[FFT_SIZE];
|
constexpr float PI = 3.14159;
|
||||||
|
|
||||||
constexpr float M_PI = 3.14159;
|
// In-place FFT on array of Complex numbers
|
||||||
|
void fft(Complex* x, int N, Complex* buffer) {
|
||||||
|
if (N <= 1) return;
|
||||||
|
|
||||||
|
Complex* even = buffer;
|
||||||
|
Complex* odd = buffer + N / 2;
|
||||||
|
|
||||||
// Call once before use
|
for (int i = 0; i < N / 2; ++i) {
|
||||||
void init_hamming_window() {
|
even[i] = x[i * 2];
|
||||||
for (int i = 0; i < FFT_SIZE; i++) {
|
odd[i] = x[i * 2 + 1];
|
||||||
window[i] = 0.54f - 0.46f * cosf(2.0f * (float)M_PI * i / (FFT_SIZE - 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int bit_reverse(unsigned int x, int log2n) {
|
|
||||||
unsigned int n = 0;
|
|
||||||
for (int i = 0; i < log2n; i++) {
|
|
||||||
n <<= 1;
|
|
||||||
n |= (x & 1);
|
|
||||||
x >>= 1;
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
void compute_fft(float* time_data, float* freq_out) {
|
|
||||||
static float real[FFT_SIZE];
|
|
||||||
static float imag[FFT_SIZE];
|
|
||||||
|
|
||||||
int log2n = 0;
|
|
||||||
for (int t = FFT_SIZE; t > 1; t >>= 1) ++log2n;
|
|
||||||
|
|
||||||
// Apply Hamming window
|
|
||||||
for (int i = 0; i < FFT_SIZE; i++) {
|
|
||||||
real[i] = time_data[i] * window[i];
|
|
||||||
imag[i] = 0.0f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bit reversal
|
fft(even, N / 2, buffer + N); // deeper even
|
||||||
for (int i = 0; i < FFT_SIZE; ++i) {
|
fft(odd, N / 2, buffer + N + N / 2); // deeper odd
|
||||||
int j = bit_reverse(i, log2n);
|
|
||||||
if (j > i) {
|
for (int k = 0; k < N / 2; ++k) {
|
||||||
float tmp_re = real[i], tmp_im = imag[i];
|
double angle = -2 * PI * k / N;
|
||||||
real[i] = real[j]; imag[i] = imag[j];
|
Complex twiddle(cos(angle), sin(angle));
|
||||||
real[j] = tmp_re; imag[j] = tmp_im;
|
Complex t = twiddle * odd[k];
|
||||||
}
|
x[k] = even[k] + t;
|
||||||
|
x[k + N / 2] = even[k] - t;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Cooley-Tukey FFT
|
|
||||||
for (int s = 1; s <= log2n; ++s) {
|
|
||||||
int m = 1 << s;
|
|
||||||
for (int k = 0; k < FFT_SIZE; k += m) {
|
|
||||||
for (int j = 0; j < m / 2; ++j) {
|
|
||||||
int t = k + j;
|
|
||||||
int u = t + m / 2;
|
|
||||||
|
|
||||||
float angle = -2.0f * (float)M_PI * j / m;
|
|
||||||
float w_real = cosf(angle);
|
|
||||||
float w_imag = sinf(angle);
|
|
||||||
|
|
||||||
float re = w_real * real[u] - w_imag * imag[u];
|
|
||||||
float im = w_real * imag[u] + w_imag * real[u];
|
|
||||||
|
|
||||||
real[u] = real[t] - re;
|
|
||||||
imag[u] = imag[t] - im;
|
|
||||||
real[t] += re;
|
|
||||||
imag[t] += im;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < FFT_SIZE / 2; ++i) {
|
|
||||||
float mag = sqrtf(real[i] * real[i] + imag[i] * imag[i]) / FFT_SIZE;
|
|
||||||
float db = 20.0f * log10f(mag + 1e-6f); // Decibels
|
|
||||||
float normalized = (db + 60.0f) / 60.0f; // [0,1]
|
|
||||||
freq_out[i] = mag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
15
src/fft.h
15
src/fft.h
@ -3,5 +3,16 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define FFT_SIZE 2048
|
#define FFT_SIZE 2048
|
||||||
void init_hamming_window();
|
// Simple complex number struct
|
||||||
void compute_fft(float* time_data, float* freq_out);
|
struct Complex {
|
||||||
|
float re, im;
|
||||||
|
Complex(float r = 0, float i = 0) : re(r), im(i) {}
|
||||||
|
Complex operator+(const Complex& o) const { return { re + o.re, im + o.im }; }
|
||||||
|
Complex operator-(const Complex& o) const { return { re - o.re, im - o.im }; }
|
||||||
|
Complex operator*(const Complex& o) const {
|
||||||
|
return { re * o.re - im * o.im, re * o.im + im * o.re };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void fft(Complex* x, int N, Complex* buffer);
|
||||||
|
|
||||||
|
|||||||
37
src/main.cpp
37
src/main.cpp
@ -36,14 +36,25 @@
|
|||||||
|
|
||||||
#include "fft.h"
|
#include "fft.h"
|
||||||
|
|
||||||
|
|
||||||
#pragma data_seg(".pids")
|
#pragma data_seg(".pids")
|
||||||
// static allocation saves a few bytes
|
// static allocation saves a few bytes
|
||||||
static int pidMain;
|
static int pidMain;
|
||||||
static int pidPost;
|
static int pidPost;
|
||||||
// static HDC hDC;
|
// static HDC hDC;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef EDITOR_CONTROLS
|
#ifndef EDITOR_CONTROLS
|
||||||
#pragma code_seg(".main")
|
#pragma code_seg(".main")
|
||||||
|
// FFT buffers
|
||||||
|
static Complex signal[FFT_SIZE];
|
||||||
|
static Complex buffer[3 * FFT_SIZE];
|
||||||
|
|
||||||
|
static float fft_uniform[FFT_SIZE / 4];
|
||||||
|
static float shapesData[SHAPES_TEX_SIZE];
|
||||||
|
|
||||||
void entrypoint(void)
|
void entrypoint(void)
|
||||||
#else
|
#else
|
||||||
#include "editor.h"
|
#include "editor.h"
|
||||||
@ -112,7 +123,7 @@ int __cdecl main(int argc, char* argv[])
|
|||||||
track.play();
|
track.play();
|
||||||
double position = 0.0;
|
double position = 0.0;
|
||||||
#endif
|
#endif
|
||||||
|
static float syncs[1 + SU_NUMSYNCS];
|
||||||
long playCursor = 0;
|
long playCursor = 0;
|
||||||
long lastPlayCursor = -1;
|
long lastPlayCursor = -1;
|
||||||
volatile float maximum = 0.0; // Helper variable to calculate maximum fft output for normalization
|
volatile float maximum = 0.0; // Helper variable to calculate maximum fft output for normalization
|
||||||
@ -123,17 +134,6 @@ int __cdecl main(int argc, char* argv[])
|
|||||||
// Play sound
|
// Play sound
|
||||||
direct_sound_buffer->Play(0, 0, 0);
|
direct_sound_buffer->Play(0, 0, 0);
|
||||||
|
|
||||||
static float syncs[1 + SU_NUMSYNCS];
|
|
||||||
|
|
||||||
// Init FFT
|
|
||||||
init_hamming_window();
|
|
||||||
// FFT buffers
|
|
||||||
static float fft_input[FFT_SIZE];
|
|
||||||
static float fft_output[FFT_SIZE / 2]; // Magnitudes
|
|
||||||
static float fft_uniform[FFT_SIZE / 4];
|
|
||||||
|
|
||||||
static float shapesData[SHAPES_TEX_SIZE];
|
|
||||||
|
|
||||||
// main note effect
|
// main note effect
|
||||||
boolean beenPlaying = false;
|
boolean beenPlaying = false;
|
||||||
const int SHAPES_SIZE = 15;
|
const int SHAPES_SIZE = 15;
|
||||||
@ -221,7 +221,7 @@ int __cdecl main(int argc, char* argv[])
|
|||||||
{
|
{
|
||||||
SUsample* samples = (SUsample*)audio_ptr;
|
SUsample* samples = (SUsample*)audio_ptr;
|
||||||
for (int i = 0; i < FFT_SIZE; ++i) {
|
for (int i = 0; i < FFT_SIZE; ++i) {
|
||||||
fft_input[i] = (float)samples[i];
|
signal[i] = Complex((float)samples[i], 0.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,20 +229,23 @@ int __cdecl main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate FFT
|
// Calculate FFT
|
||||||
compute_fft(fft_input, fft_output);
|
fft(signal, FFT_SIZE, buffer);
|
||||||
|
|
||||||
// Normalize output
|
// Normalize output
|
||||||
for (int i = 0; i < (FFT_SIZE / 4); i++)
|
for (int i = 0; i < (FFT_SIZE / 4); i++)
|
||||||
{
|
{
|
||||||
float gain = 50.0f;
|
float gain = 0.025f;
|
||||||
float alpha = 0.10f; // "Hidastaa" FFT:n piikkej<EFBFBD>
|
float alpha = 0.15f; // "Hidastaa" FFT:n piikkejä
|
||||||
float threshhold = 0.015f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
|
float threshhold = 0.015f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
|
||||||
float x_t = (fft_output[i] < threshhold) ? 0.f : fft_output[i] * gain;
|
float magnitude = sqrt(signal[i].re * signal[i].re + signal[i].im * signal[i].im);
|
||||||
|
|
||||||
|
float x_t = (magnitude < threshhold) ? 0.f : magnitude * gain;
|
||||||
// Exponential smoothing kaava
|
// Exponential smoothing kaava
|
||||||
// s(t) = alpha*x(t)+(1-alpha)*s(t-1)
|
// s(t) = alpha*x(t)+(1-alpha)*s(t-1)
|
||||||
fft_uniform[i] = alpha * (x_t)+(1 - alpha) * fft_uniform[i];
|
fft_uniform[i] = alpha * (x_t)+(1 - alpha) * fft_uniform[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncs[0] = (float)playCursor / (SU_SAMPLE_RATE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE); // Aika sekunteina.
|
syncs[0] = (float)playCursor / (SU_SAMPLE_RATE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE); // Aika sekunteina.
|
||||||
|
|
||||||
for (int i = 0; i < SU_NUMSYNCS; ++i)
|
for (int i = 0; i < SU_NUMSYNCS; ++i)
|
||||||
|
|||||||
Reference in New Issue
Block a user