Skip to content

Instantly share code, notes, and snippets.

@svenoaks
Created May 26, 2023 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenoaks/5847dd0b0b7d79136535b79a9f5c7cc3 to your computer and use it in GitHub Desktop.
Save svenoaks/5847dd0b0b7d79136535b79a9f5c7cc3 to your computer and use it in GitHub Desktop.
FFT Test
void FFTTest() {
AudioFile<float> audioFile, audioFileReference, audioFileResult;
string filename = "../" + std::string("test1mono") + ".wav";
string filenameResult = "../" + std::string("fftResult") + ".wav";
audioFile.load(filename);
using namespace SMP;
FormantFFTProcessor m_fftProcessor;
// Prepare output vector
std::vector<float> output_samples(audioFile.getNumSamplesPerChannel(), 0.0f);
// Iterate over the input in chunks of FORMANT_TIME_DOMAIN_FFT_LENGTH samples
for (size_t i = 0; i < audioFile.getNumSamplesPerChannel(); i += FORMANT_TIME_DOMAIN_FFT_LENGTH) {
// Prepare the chunk
std::vector<float, FFTAllocator> chunk(FORMANT_TIME_DOMAIN_FFT_LENGTH, 0.0f);
for (size_t j = 0; j < FORMANT_TIME_DOMAIN_FFT_LENGTH; ++j) {
if (i + j < audioFile.getNumSamplesPerChannel()) {
chunk[j] = audioFile.samples[0][i + j];
#ifdef USE_ACCELERATE_FFT
chunk[j] *= 0.5f;
#elif defined(USE_AV_FFT)
chunk[j] *= 2.0f;
#endif
}
}
// Prepare complex vectors for FFT
//std::vector<ComplexType> dft(FORMANT_TIME_DOMAIN_FFT_LENGTH, ComplexType{});
ARRAY_WRAPPER<SMP::ComplexType, FORMANT_FREQ_DOMAIN_FFT_LENGTH> dft_transfer{SMP::ComplexType{}};
// Apply forward FFT
m_fftProcessor.processForwardFFT(chunk, dft_transfer);
// Apply inverse FFT
m_fftProcessor.processInverseFFT(dft_transfer, chunk);
// Apply scaling
float norm_factor = 1.0 / FORMANT_TIME_DOMAIN_FFT_LENGTH;
for (float &sample : chunk) {
sample *= norm_factor;
}
// Save the chunk into the output vector
for (size_t j = 0; j < FORMANT_TIME_DOMAIN_FFT_LENGTH; ++j) {
if (i + j < output_samples.size()) {
output_samples[i + j] = chunk[j];
}
}
}
// Replace input samples with output samples
audioFile.samples[0] = output_samples;
audioFile.save(filenameResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment