3 #ifndef SPECTMORPH_MATH_HH 4 #define SPECTMORPH_MATH_HH 13 #include <xmmintrin.h> 46 template<
class Iterator,
int MODE>
48 internal_fast_vector_sin (
const VectorSinParams& params, Iterator begin, Iterator end)
50 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
52 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
53 const double inc_re = cos (phase_inc);
54 const double inc_im = sin (phase_inc);
60 sincos (params.
phase, &state_im, &state_re);
61 state_re *= params.
mag;
62 state_im *= params.
mag;
64 for (Iterator x = begin; x != end; x++)
70 if ((n++ & 255) == 255)
72 sincos (phase_inc * n + params.
phase, &state_im, &state_re);
73 state_re *= params.
mag;
74 state_im *= params.
mag;
82 const double re = state_re * inc_re - state_im * inc_im;
83 const double im = state_re * inc_im + state_im * inc_re;
90 template<
class Iterator,
int MODE>
92 internal_fast_vector_sincos (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end, Iterator cos_begin)
94 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
96 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
97 const double inc_re = cos (phase_inc);
98 const double inc_im = sin (phase_inc);
104 sincos (params.
phase, &state_im, &state_re);
105 state_re *= params.
mag;
106 state_im *= params.
mag;
108 for (Iterator x = sin_begin, y = cos_begin; x != sin_end; x++, y++)
120 if ((n++ & 255) == 255)
122 sincos (phase_inc * n + params.
phase, &state_im, &state_re);
123 state_re *= params.
mag;
124 state_im *= params.
mag;
132 const double re = state_re * inc_re - state_im * inc_im;
133 const double im = state_re * inc_im + state_im * inc_re;
140 template<
class Iterator>
142 fast_vector_sin (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end)
146 internal_fast_vector_sin<Iterator, VectorSinParams::ADD> (params, sin_begin, sin_end);
150 internal_fast_vector_sin<Iterator, VectorSinParams::REPLACE> (params, sin_begin, sin_end);
154 g_assert_not_reached();
158 template<
class Iterator>
160 fast_vector_sincos (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end, Iterator cos_begin)
164 internal_fast_vector_sincos<Iterator, VectorSinParams::ADD> (params, sin_begin, sin_end, cos_begin);
168 internal_fast_vector_sincos<Iterator, VectorSinParams::REPLACE> (params, sin_begin, sin_end, cos_begin);
172 g_assert_not_reached();
188 template<
bool NEED_COS,
int MODE>
190 internal_fast_vector_sincosf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end,
float *cos_begin)
193 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
195 const int TABLE_SIZE = 32;
197 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
198 const double inc_re16 = cos (phase_inc * TABLE_SIZE * 4);
199 const double inc_im16 = sin (phase_inc * TABLE_SIZE * 4);
205 sincos (params.
phase, &state_im, &state_re);
206 state_re *= params.
mag;
207 state_im *= params.
mag;
209 F4Vector incf_re[TABLE_SIZE];
210 F4Vector incf_im[TABLE_SIZE];
214 table_params.
phase = 0;
215 table_params.
mag = 1;
217 fast_vector_sincos (table_params, incf_im[0].f, incf_im[0].f + (TABLE_SIZE * 4), incf_re[0].f);
220 int todo = sin_end - sin_begin;
221 while (todo >= 4 * TABLE_SIZE)
225 sf_re.f[0] = state_re;
226 sf_re.f[1] = state_re;
227 sf_re.f[2] = state_re;
228 sf_re.f[3] = state_re;
229 sf_im.f[0] = state_im;
230 sf_im.f[1] = state_im;
231 sf_im.f[2] = state_im;
232 sf_im.f[3] = state_im;
240 F4Vector *new_im =
reinterpret_cast<F4Vector *
> (sin_begin + n);
241 F4Vector *new_re =
reinterpret_cast<F4Vector *
> (cos_begin + n);
242 for (
int k = 0; k < TABLE_SIZE; k++)
248 new_re[k].v = _mm_add_ps (new_re[k].v, _mm_sub_ps (_mm_mul_ps (sf_re.v, incf_re[k].v),
249 _mm_mul_ps (sf_im.v, incf_im[k].v)));
251 new_im[k].v = _mm_add_ps (new_im[k].v, _mm_add_ps (_mm_mul_ps (sf_re.v, incf_im[k].v),
252 _mm_mul_ps (sf_im.v, incf_re[k].v)));
258 new_re[k].v = _mm_sub_ps (_mm_mul_ps (sf_re.v, incf_re[k].v),
259 _mm_mul_ps (sf_im.v, incf_im[k].v));
261 new_im[k].v = _mm_add_ps (_mm_mul_ps (sf_re.v, incf_im[k].v),
262 _mm_mul_ps (sf_im.v, incf_re[k].v));
272 const double re = state_re * inc_re16 - state_im * inc_im16;
273 const double im = state_re * inc_im16 + state_im * inc_re16;
277 todo -= 4 * TABLE_SIZE;
282 rest_params.
phase += n * phase_inc;
284 fast_vector_sincos (rest_params, sin_begin + n, sin_end, cos_begin + n);
286 fast_vector_sin (rest_params, sin_begin + n, sin_end);
289 fast_vector_sincos (params, sin_begin, sin_end, cos_begin);
291 fast_vector_sin (params, sin_begin, sin_end);
296 fast_vector_sincosf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end,
float *cos_begin)
300 internal_fast_vector_sincosf<true, VectorSinParams::ADD> (params, sin_begin, sin_end, cos_begin);
304 internal_fast_vector_sincosf<true, VectorSinParams::REPLACE> (params, sin_begin, sin_end, cos_begin);
308 g_assert_not_reached();
313 fast_vector_sinf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end)
317 internal_fast_vector_sincosf<false, VectorSinParams::ADD> (params, sin_begin, sin_end, NULL);
321 internal_fast_vector_sincosf<false, VectorSinParams::REPLACE> (params, sin_begin, sin_end, NULL);
325 g_assert_not_reached();
330 zero_float_block (
size_t n_values,
float *values)
332 memset (values, 0, n_values *
sizeof (
float));
338 extern float *int_sincos_table;
340 return int_sincos_table[i];
346 extern float *int_sincos_table;
349 return int_sincos_table[i];
355 extern float *int_sincos_table;
357 int_sincos_table = (
float *) malloc (
sizeof (
float) * 256);
358 for (
int i = 0; i < 256; i++)
359 int_sincos_table[i] = sin (
double (i / 256.0) * 2 * M_PI);
365 window_cos (
double x)
369 return 0.5 * cos (x * M_PI) + 0.5;
373 window_hamming (
double x)
378 return 0.54 + 0.46 * cos (M_PI * x);
382 window_blackman (
double x)
386 return 0.42 + 0.5 * cos (M_PI * x) + 0.08 * cos (2.0 * M_PI * x);
390 window_blackman_harris_92 (
double x)
395 const double a0 = 0.35875, a1 = 0.48829, a2 = 0.14128, a3 = 0.01168;
397 return a0 + a1 * cos (M_PI * x) + a2 * cos (2.0 * M_PI * x) + a3 * cos (3.0 * M_PI * x);
401 double db_to_factor (
double dB);
402 double db_from_factor (
double factor,
double min_dB);
404 #if defined (__i386__) && defined (__GNUC__) 405 static inline int G_GNUC_CONST
406 sm_ftoi (
register float f)
415 static inline int G_GNUC_CONST
416 sm_dtoi (
register double f)
426 sm_round_positive (
double d)
432 sm_round_positive (
float f)
438 sm_round_positive (
double d)
440 return int (d + 0.5);
444 sm_round_positive (
float f)
446 return int (f + 0.5);
450 int sm_fpu_okround();
454 static float idb2f_high[256];
455 static float idb2f_low[256];
457 static float ifreq2f_high[256];
458 static float ifreq2f_low[256];
461 #define SM_IDB_CONST_M96 uint16_t ((512 - 96) * 64) 463 int sm_factor2delta_idb (
double factor);
464 double sm_idb2factor_slow (uint16_t idb);
468 uint16_t sm_freq2ifreq (
double freq);
469 double sm_ifreq2freq_slow (uint16_t ifreq);
472 sm_idb2factor (uint16_t idb)
474 return MathTables::idb2f_high[idb >> 8] * MathTables::idb2f_low[idb & 0xff];
478 sm_ifreq2freq (uint16_t ifreq)
480 return MathTables::ifreq2f_high[ifreq >> 8] * MathTables::ifreq2f_low[ifreq & 0xff];
484 sm_factor2idb (
double factor)
492 const double db = 20 * log10 (std::max (factor, 1e-25));
494 return sm_round_positive (db * 64 + 512 * 64);
497 double sm_lowpass1_factor (
double mix_freq,
double freq);
498 double sm_xparam (
double x,
double slope);
499 double sm_xparam_inv (
double x,
double slope);
501 double sm_bessel_i0 (
double x);
505 sm_bound (
const T& min_value,
const T& value,
const T& max_value)
507 return std::min (std::max (value, min_value), max_value);
Definition: smmath.hh:452
double freq
the frequency of the sin (and cos) wave to be created
Definition: smmath.hh:26
double phase
the start phase of the wave
Definition: smmath.hh:27
replace values in the output array with computed values
Definition: smmath.hh:33
double mag
the magnitude (amplitude) of the wave
Definition: smmath.hh:28
parameter structure for the various optimized vector sine functions
Definition: smmath.hh:22
Definition: smalignedarray.cc:7
double mix_freq
the mix freq (sampling rate) of the sin (and cos) wave to be created
Definition: smmath.hh:25
enum SpectMorph::VectorSinParams::@2 mode
whether to overwrite or add output
add computed values to the values that are in the output array
Definition: smmath.hh:32