Commit 5a6230f7 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Move FFT stuff to extra source file.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent c2d3e569
bin_PROGRAMS = audiowmark
audiowmark_SOURCES = audiowmark.cc wavdata.cc
audiowmark_SOURCES = audiowmark.cc wavdata.cc fft.cc
audiowmark_LDFLAGS = $(SNDFILE_LIBS) $(FFTW_LIBS)
......@@ -4,8 +4,7 @@
#include <random>
#include <complex>
#include <fftw3.h>
#include "fft.hh"
#include "wavdata.hh"
using std::string;
......@@ -70,52 +69,6 @@ get_frame (WavData& wav_data, int f, int ch)
return result;
}
float *
new_array_float (size_t N)
{
const size_t N_2 = N + 2; /* extra space for r2c extra complex output */
return (float *) fftwf_malloc (sizeof (float) * N_2);
}
void
free_array_float (float *f)
{
fftwf_free (f);
}
void
fftar_float (size_t N, float *in, float *out)
{
static fftwf_plan plan = nullptr; // FIXME: should be one plan per fft size
if (!plan)
{
float *plan_in = new_array_float (N);
float *plan_out = new_array_float (N);
plan = fftwf_plan_dft_r2c_1d (N, plan_in, (fftwf_complex *) plan_out, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
// we add code for saving plans here, and use patient planning
}
fftwf_execute_dft_r2c (plan, in, (fftwf_complex *) out);
}
void
fftsr_float (size_t N, float *in, float *out)
{
static fftwf_plan plan = nullptr; // FIXME: should be one plan per fft size
if (!plan)
{
float *plan_in = new_array_float (N);
float *plan_out = new_array_float (N);
plan = fftwf_plan_dft_c2r_1d (N, (fftwf_complex *) plan_in, plan_out, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
// we add code for saving plans here, and use patient planning
}
fftwf_execute_dft_c2r (plan, (fftwf_complex *)in, out);
}
void
get_up_down (int f, vector<int>& up, vector<int>& down)
{
......@@ -193,48 +146,6 @@ bit_vec_to_str (const vector<int>& bit_vec)
return bit_str;
}
vector<complex<float>>
fft (const vector<float>& in)
{
vector<complex<float>> out (in.size() / 2 + 1);
/* ensure memory is SSE-aligned (or other vectorized stuff) */
float *fft_in = new_array_float (in.size());
float *fft_out = new_array_float (in.size());
std::copy (in.begin(), in.end(), fft_in);
fftar_float (in.size(), fft_in, fft_out);
/* complex<float> vector and fft_out have the same layout in memory */
std::copy (fft_out, fft_out + out.size() * 2, reinterpret_cast<float *> (&out[0]));
free_array_float (fft_out);
free_array_float (fft_in);
return out;
}
vector<float>
ifft (const vector<complex<float>>& in)
{
vector<float> out ((in.size() - 1) * 2);
/* ensure memory is SSE-aligned (or other vectorized stuff) */
float *ifft_in = new_array_float (out.size());
float *ifft_out = new_array_float (out.size());
/* complex<float> vector and fft_out have the same layout in memory */
std::copy (in.begin(), in.end(), reinterpret_cast<complex<float> *> (ifft_in));
fftsr_float (out.size(), ifft_in, ifft_out);
std::copy (ifft_out, ifft_out + out.size(), &out[0]);
free_array_float (ifft_out);
free_array_float (ifft_in);
return out;
}
int
add_watermark (const string& infile, const string& outfile, const string& bits)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment