0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 19:23:51 +01:00
Files
fm95/lib/filters.c

39 lines
1.0 KiB
C

#include "filters.h"
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate) {
filter->prev_sample = 0.0f;
filter->alpha = exp(-1 / (tau*sample_rate));
}
float apply_preemphasis(ResistorCapacitor *filter, float sample) {
float out = sample-filter->alpha*filter->prev_sample;
filter->prev_sample = sample;
return out;
}
float hard_clip(float sample, float threshold) {
if (sample > threshold) {
return threshold; // Clip to the upper threshold
} else if (sample < -threshold) {
return -threshold; // Clip to the lower threshold
} else {
return sample; // No clipping
}
}
float voltage_db_to_voltage(float db) {
return powf(10.0f, db / 20.0f);
}
float power_db_to_voltage(float db) {
return powf(10.0f, db / 10.0f);
}
float voltage_to_voltage_db(float linear) {
return 20.0f * log10f(fmaxf(linear, 1e-10f)); // Avoid log(0)
}
float voltage_to_power_db(float linear) {
return 10.0f * log10f(fmaxf(linear, 1e-10f)); // Avoid log(0)
}