summaryrefslogtreecommitdiffstats
path: root/lib/mesa_sine_wave.c
diff options
context:
space:
mode:
authorEthan Werner <mail@ethanwerner.com>2026-08-09 15:02:21 +0400
committerEthan Werner <mail@ethanwerner.com>2026-08-09 15:02:21 +0400
commit3d92926a902017e7cf1f5738e99ff62da596ce12 (patch)
tree2b55422c26a48759d56fa91f06df24449e9dfa88 /lib/mesa_sine_wave.c
parent07e18043145f3d91c8ea7ec3ad41b3c905585277 (diff)
Diffstat (limited to 'lib/mesa_sine_wave.c')
-rw-r--r--lib/mesa_sine_wave.c50
1 files changed, 25 insertions, 25 deletions
diff --git a/lib/mesa_sine_wave.c b/lib/mesa_sine_wave.c
index 8ed5fa6..91fbcd2 100644
--- a/lib/mesa_sine_wave.c
+++ b/lib/mesa_sine_wave.c
@@ -24,7 +24,7 @@ ts_parameter_t const ts_mesa_sine_wave_p = { .n_inputs = 1, .n_outputs = 2, .n_c
uint_t ts_mesa_sine_wave_offset( ts_constant_t const *c )
{
( void )c;
- return ( c ? ((ts_mesa_sine_wave_params_t *)c)->k : 0 );
+ return ( c ? ((ts_mesa_sine_wave_params_t const *)c)->k : 0 );
}
int ts_mesa_sine_wave(
@@ -35,64 +35,64 @@ int ts_mesa_sine_wave(
{
TS_PROLOGUE( ts_mesa_sine_wave_p );
if ( !c ) return -1;
- uint_t k = ((ts_mesa_sine_wave_params_t *)c)->k;
+ uint_t k = ((ts_mesa_sine_wave_params_t const *)c)->k;
if (k < 2 || n < k) return -1;
- fp_t *sma = calloc(n, sizeof(fp_t));
- if (!sma) return -1;
+ fp_t *sma = calloc( n, sizeof( fp_t ) );
+ if ( !sma ) return -1;
- for (uint_t i = 0; i < n; ++i)
+ for ( uint_t i = 0; i < n; ++i )
{
- if (i < k - 1)
+ if ( i < k - 1 )
{
fp_t sum = 0;
- for (uint_t j = 0; j <= i; ++j)
+ for ( uint_t j = 0; j <= i; ++j )
sum += x[0][j];
- sma[i] = sum / (i + 1);
+ sma[i] = sum / (fp_t)( i + 1 );
}
else
{
fp_t sum = 0;
- for (uint_t j = i - k + 1; j <= i; ++j)
+ for ( uint_t j = i - k + 1; j <= i; ++j )
sum += x[0][j];
- sma[i] = sum / k;
+ sma[i] = sum / (fp_t)k;
}
}
fp_t ema_roc = 0;
uint_t ema_period = 5;
- fp_t ema_a = 2.0 / (ema_period + 1);
+ fp_t ema_a = 2.0 / ( (fp_t)ema_period + 1 );
fp_t phase = 0;
int_t first = 1;
- for (uint_t j = 0; j < n && j < k; ++j) { y[0][j] = 0; y[1][j] = 0; }
- for (uint_t i = k; i < n; ++i)
+ for ( uint_t j = 0; j < n && j < k; ++j ) { y[0][j] = 0; y[1][j] = 0; }
+ for ( uint_t i = k; i < n; ++i )
{
fp_t roc = sma[i] - sma[i - 2];
- if (first)
+ if ( first )
{
ema_roc = roc;
first = 0;
}
else
{
- ema_roc = ema_roc + ema_a * (roc - ema_roc);
+ ema_roc = ema_roc + ema_a * ( roc - ema_roc );
}
fp_t smoothed = ema_roc;
- if (smoothed > 1.0) smoothed = 1.0;
- if (smoothed < 0.01) smoothed = 0.01;
+ if ( smoothed > 1.0 ) smoothed = 1.0;
+ if ( smoothed < 0.01 ) smoothed = 0.01;
fp_t wperiod = 2 * PI / smoothed;
- if (wperiod < 5) wperiod = 5;
- if (wperiod > 12) wperiod = 12;
+ if ( wperiod < 5 ) wperiod = 5;
+ if ( wperiod > 12 ) wperiod = 12;
- if (smoothed >= 1.0)
+ if ( smoothed >= 1.0 )
{
phase = 0;
}
- else if (smoothed <= 0.01)
+ else if ( smoothed <= 0.01 )
{
phase = 2 * PI;
}
@@ -100,13 +100,13 @@ int ts_mesa_sine_wave(
{
phase += 2 * PI / wperiod;
}
- while (phase > 2 * PI) phase -= 2 * PI;
+ while ( phase > 2 * PI ) phase -= 2 * PI;
- y[0][i] = cos(phase);
- y[1][i] = cos(phase + PI / 2);
+ y[0][i] = cos( phase );
+ y[1][i] = cos( phase + PI / 2 );
}
- free(sma);
+ free( sma );
return 0;
}