Rename filter -> iir_filter (file name and instance name)

This is useful when using fir_filter in the future.
pull/1/head
Andreas Eversberg 6 years ago
parent d03443c4f1
commit ca10048e5c

@ -88,7 +88,7 @@ typedef struct cnetz {
/* dsp states */
enum dsp_mode dsp_mode; /* current mode: audio, "Telegramm", .... */
filter_t lp; /* low pass filter to eliminate noise above 5280 Hz */
iir_filter_t lp; /* low pass filter to eliminate noise above 5280 Hz */
fsk_fm_demod_t fsk_demod; /* demod process */
double fsk_deviation; /* deviation of FSK signal on sound card */
sample_t fsk_ramp_up[256]; /* samples of upward ramp shape */

@ -123,7 +123,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2])
dsp_init_ramp(cnetz);
/* init low pass filter for received signal */
filter_lowpass_init(&cnetz->lp, MAX_MODULATION, cnetz->sender.samplerate, 2);
iir_lowpass_init(&cnetz->lp, MAX_MODULATION, cnetz->sender.samplerate, 2);
/* create speech buffer */
cnetz->dsp_speech_buffer = calloc(sizeof(sample_t), cnetz->sender.samplerate); /* buffer is greater than sr/1.1, just to be secure */
@ -134,7 +134,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2])
}
/* reinit the sample rate to shrink/expand audio */
init_samplerate(&cnetz->sender.srstate, 0x8000, (double)cnetz->sender.samplerate / 1.1); /* 66 <-> 60 */
init_samplerate(&cnetz->sender.srstate, 8000.0, (double)cnetz->sender.samplerate / 1.1, 3300.0); /* 66 <-> 60 */
rc = fsk_fm_init(&cnetz->fsk_demod, cnetz, cnetz->sender.samplerate, (double)BITRATE / (1.0 + clock_speed[0] / 1000000.0));
if (rc < 0)
@ -584,7 +584,7 @@ void sender_receive(sender_t *sender, sample_t *samples, int length)
#endif
if (cnetz->dsp_mode != DSP_MODE_OFF) {
filter_process(&cnetz->lp, samples, length);
iir_process(&cnetz->lp, samples, length);
fsk_fm_demod(&cnetz->fsk_demod, samples, length);
}
return;

@ -50,7 +50,7 @@ void scrambler_init(void)
void scrambler_setup(scrambler_t *scrambler, int samplerate)
{
filter_lowpass_init(&scrambler->lp, CARRIER_HZ - FILTER_BELOW, samplerate, FILTER_TURNS);
iir_lowpass_init(&scrambler->lp, CARRIER_HZ - FILTER_BELOW, samplerate, FILTER_TURNS);
scrambler->carrier_phaseshift65536 = 65536.0 / ((double)samplerate / CARRIER_HZ);
}
@ -77,7 +77,7 @@ void scrambler(scrambler_t *scrambler, sample_t *samples, int length)
scrambler->carrier_phase65536 = phase;
/* cut off carrier frequency and modulation above carrier frequency */
filter_process(&scrambler->lp, samples, length);
iir_process(&scrambler->lp, samples, length);
}

@ -1,9 +1,9 @@
#include "../common/filter.h"
#include "../common/iir_filter.h"
typedef struct scrambler {
double carrier_phaseshift65536;/* carrier phase shift per sample */
double carrier_phase65536; /* current phase of carrier frequency */
filter_t lp; /* filter to remove carrier frequency */
iir_filter_t lp; /* filter to remove carrier frequency */
} scrambler_t;
void scrambler_init(void);

@ -12,7 +12,7 @@ libcommon_a_SOURCES = \
../common/goertzel.c \
../common/jitter.c \
../common/loss.c \
../common/filter.c \
../common/iir_filter.c \
../common/dtmf.c \
../common/samplerate.c \
../common/call.c \

@ -497,7 +497,7 @@ int call_init(const char *station_id, const char *audiodev, int samplerate, int
if (!audiodev[0])
return 0;
rc = init_samplerate(&call.srstate, 8000.0, (double)samplerate);
rc = init_samplerate(&call.srstate, 8000.0, (double)samplerate, 3300.0);
if (rc < 0) {
PDEBUG(DSENDER, DEBUG_ERROR, "Failed to init sample rate conversion!\n");
goto error;

@ -22,7 +22,7 @@
#include <string.h>
#include <math.h>
#include "sample.h"
#include "filter.h"
#include "iir_filter.h"
#include "emphasis.h"
#include "debug.h"
@ -66,7 +66,7 @@ int init_emphasis(emphasis_t *state, int samplerate, double cut_off)
state->d.factor = factor;
state->d.amp = 1.0;
filter_highpass_init(&state->d.hp, CUT_OFF_H, samplerate, 1);
iir_highpass_init(&state->d.hp, CUT_OFF_H, samplerate, 1);
/* calibrate amplification to be neutral at 1000 Hz */
gen_sine(test_samples, sizeof(test_samples) / sizeof(test_samples[0]), samplerate, 1000.0);
@ -128,6 +128,6 @@ void de_emphasis(emphasis_t *state, sample_t *samples, int num)
/* high pass filter to remove DC and low frequencies */
void dc_filter(emphasis_t *state, sample_t *samples, int num)
{
filter_process(&state->d.hp, samples, num);
iir_process(&state->d.hp, samples, num);
}

@ -5,7 +5,7 @@ typedef struct emphasis {
double amp;
} p;
struct {
filter_t hp;
iir_filter_t hp;
double y_last;
double factor;
double amp;

@ -1,99 +0,0 @@
/* cut-off filter (biquad) based on Nigel Redmon (www.earlevel.com)
*
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "sample.h"
#include "filter.h"
#define PI M_PI
void filter_lowpass_init(filter_t *bq, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
if (iterations > 64) {
fprintf(stderr, "%s failed: too many iterations, please fix!\n", __func__);
abort();
}
memset(bq, 0, sizeof(*bq));
bq->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
bq->a0 = K * K * norm;
bq->a1 = 2 * bq->a0;
bq->a2 = bq->a0;
bq->b1 = 2 * (K * K - 1) * norm;
bq->b2 = (1 - K / Q + K * K) * norm;
}
void filter_highpass_init(filter_t *bq, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
memset(bq, 0, sizeof(*bq));
bq->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
bq->a0 = 1 * norm;
bq->a1 = -2 * bq->a0;
bq->a2 = bq->a0;
bq->b1 = 2 * (K * K - 1) * norm;
bq->b2 = (1 - K / Q + K * K) * norm;
}
void filter_process(filter_t *bq, sample_t *samples, int length)
{
double a0, a1, a2, b1, b2;
double *z1, *z2;
double in, out;
int iterations = bq->iter;
int i, j;
/* get states */
a0 = bq->a0;
a1 = bq->a1;
a2 = bq->a2;
b1 = bq->b1;
b2 = bq->b2;
z1 = bq->z1;
z2 = bq->z2;
/* process filter */
for (i = 0; i < length; i++) {
in = *samples;
for (j = 0; j < iterations; j++) {
out = in * a0 + z1[j];
z1[j] = in * a1 + z2[j] - b1 * out;
z2[j] = in * a2 - b2 * out;
in = out;
}
*samples++ = in;
}
}

@ -1,14 +0,0 @@
#ifndef _FILTER_H
#define _FILTER_H
typedef struct filter {
int iter;
double a0, a1, a2, b1, b2;
double z1[64], z2[64];
} filter_t;
void filter_lowpass_init(filter_t *bq, double frequency, int samplerate, int iterations);
void filter_highpass_init(filter_t *bq, double frequency, int samplerate, int iterations);
void filter_process(filter_t *bq, sample_t *samples, int length);
#endif /* _FILTER_H */

@ -23,7 +23,7 @@
#include <string.h>
#include <math.h>
#include "sample.h"
#include "filter.h"
#include "iir_filter.h"
#include "fm_modulation.h"
//#define FAST_SINE
@ -110,8 +110,8 @@ void fm_demod_init(fm_demod_t *demod, double samplerate, double offset, double b
#endif
/* use fourth order (2 iter) filter, since it is as fast as second order (1 iter) filter */
filter_lowpass_init(&demod->lp[0], bandwidth / 2.0, samplerate, 2);
filter_lowpass_init(&demod->lp[1], bandwidth / 2.0, samplerate, 2);
iir_lowpass_init(&demod->lp[0], bandwidth / 2.0, samplerate, 2);
iir_lowpass_init(&demod->lp[1], bandwidth / 2.0, samplerate, 2);
#ifdef FAST_SINE
int i;
@ -169,8 +169,8 @@ void fm_demodulate(fm_demod_t *demod, sample_t *samples, int num, float *buff)
Q[s] = i * _sin + q * _cos;
}
demod->phase = phase;
filter_process(&demod->lp[0], I, num);
filter_process(&demod->lp[1], Q, num);
iir_process(&demod->lp[0], I, num);
iir_process(&demod->lp[1], Q, num);
last_phase = demod->last_phase;
for (s = 0; s < num; s++) {
phase = atan2(Q[s], I[s]);

@ -15,7 +15,7 @@ typedef struct fm_demod {
double phase; /* current rotation phase (used to shift) */
double rot; /* rotation step per sample to shift rx frequency (used to shift) */
double last_phase; /* last phase of FM (used to demodulate) */
filter_t lp[2]; /* filters received IQ signal */
iir_filter_t lp[2]; /* filters received IQ signal */
double *sin_tab; /* sine/cosine table rotation */
} fm_demod_t;

@ -0,0 +1,133 @@
/* cut-off filter (biquad) based on Nigel Redmon (www.earlevel.com)
*
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "sample.h"
#include "iir_filter.h"
#define PI M_PI
void iir_lowpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
if (iterations > 64) {
fprintf(stderr, "%s failed: too many iterations, please fix!\n", __func__);
abort();
}
memset(filter, 0, sizeof(*filter));
filter->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
filter->a0 = K * K * norm;
filter->a1 = 2 * filter->a0;
filter->a2 = filter->a0;
filter->b1 = 2 * (K * K - 1) * norm;
filter->b2 = (1 - K / Q + K * K) * norm;
}
void iir_highpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
memset(filter, 0, sizeof(*filter));
filter->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
filter->a0 = 1 * norm;
filter->a1 = -2 * filter->a0;
filter->a2 = filter->a0;
filter->b1 = 2 * (K * K - 1) * norm;
filter->b2 = (1 - K / Q + K * K) * norm;
}
void iir_bandpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
memset(filter, 0, sizeof(*filter));
filter->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
filter->a0 = K / Q * norm;
filter->a1 = 0;
filter->a2 = -filter->a0;
filter->b1 = 2 * (K * K - 1) * norm;
filter->b2 = (1 - K / Q + K * K) * norm;
}
void iir_notch_init(iir_filter_t *filter, double frequency, int samplerate, int iterations)
{
double Fc, Q, K, norm;
memset(filter, 0, sizeof(*filter));
filter->iter = iterations;
Q = pow(sqrt(0.5), 1.0 / (double)iterations); /* 0.7071 @ 1 iteration */
Fc = frequency / (double)samplerate;
K = tan(PI * Fc);
norm = 1 / (1 + K / Q + K * K);
filter->a0 = (1 + K * K) * norm;
filter->a1 = 2 * (K * K - 1) * norm;
filter->a2 = filter->a0;
filter->b1 = filter->a1;
filter->b2 = (1 - K / Q + K * K) * norm;
}
void iir_process(iir_filter_t *filter, sample_t *samples, int length)
{
double a0, a1, a2, b1, b2;
double *z1, *z2;
double in, out;
int iterations = filter->iter;
int i, j;
/* get states */
a0 = filter->a0;
a1 = filter->a1;
a2 = filter->a2;
b1 = filter->b1;
b2 = filter->b2;
z1 = filter->z1;
z2 = filter->z2;
/* process filter */
for (i = 0; i < length; i++) {
in = *samples;
for (j = 0; j < iterations; j++) {
out = in * a0 + z1[j];
z1[j] = in * a1 + z2[j] - b1 * out;
z2[j] = in * a2 - b2 * out;
in = out;
}
*samples++ = in;
}
}

@ -0,0 +1,16 @@
#ifndef _FILTER_H
#define _FILTER_H
typedef struct iir_filter {
int iter;
double a0, a1, a2, b1, b2;
double z1[64], z2[64];
} iir_filter_t;
void iir_lowpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations);
void iir_highpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations);
void iir_bandpass_init(iir_filter_t *filter, double frequency, int samplerate, int iterations);
void iir_notch_init(iir_filter_t *filter, double frequency, int samplerate, int iterations);
void iir_process(iir_filter_t *filter, sample_t *samples, int length);
#endif /* _FILTER_H */

@ -25,7 +25,7 @@
#include "sample.h"
#include "samplerate.h"
int init_samplerate(samplerate_t *state, double low_samplerate, double high_samplerate)
int init_samplerate(samplerate_t *state, double low_samplerate, double high_samplerate, double filter_cutoff)
{
memset(state, 0, sizeof(*state));
state->factor = high_samplerate / low_samplerate;
@ -34,8 +34,8 @@ int init_samplerate(samplerate_t *state, double low_samplerate, double high_samp
abort();
}
filter_lowpass_init(&state->up.lp, 3300.0, high_samplerate, 2);
filter_lowpass_init(&state->down.lp, 3300.0, high_samplerate, 2);
iir_lowpass_init(&state->up.lp, filter_cutoff, high_samplerate, 2);
iir_lowpass_init(&state->down.lp, filter_cutoff, high_samplerate, 2);
return 0;
}
@ -49,7 +49,7 @@ int samplerate_downsample(samplerate_t *state, sample_t *samples, int input_num)
sample_t last_sample;
/* filter down */
filter_process(&state->down.lp, samples, input_num);
iir_process(&state->down.lp, samples, input_num);
/* get last sample for interpolation */
last_sample = state->down.last_sample;
@ -144,7 +144,7 @@ int samplerate_upsample(samplerate_t *state, sample_t *input, int input_num, sam
state->up.in_index = in_index;
/* filter up */
filter_process(&state->up.lp, samples, output_num);
iir_process(&state->up.lp, samples, output_num);
if (input == output) {
/* copy samples */

@ -1,19 +1,19 @@
#include "filter.h"
#include "iir_filter.h"
typedef struct samplerate {
double factor;
struct {
filter_t lp;
iir_filter_t lp;
sample_t last_sample;
double in_index;
} down;
struct {
filter_t lp;
iir_filter_t lp;
sample_t last_sample;
double in_index;
} up;
} samplerate_t;
int init_samplerate(samplerate_t *state, double low_samplerate, double high_samplerate);
int init_samplerate(samplerate_t *state, double low_samplerate, double high_samplerate, double filter_cutoff);
int samplerate_downsample(samplerate_t *state, sample_t *samples, int input_num);
int samplerate_upsample(samplerate_t *state, sample_t *input, int input_num, sample_t *output);

@ -24,7 +24,7 @@
#include <errno.h>
#include <math.h>
#include "sample.h"
#include "filter.h"
#include "iir_filter.h"
#include "fm_modulation.h"
#include "sender.h"
#ifdef HAVE_UHD

@ -114,7 +114,7 @@ int sender_create(sender_t *sender, int kanal, double sendefrequenz, double empf
}
}
rc = init_samplerate(&sender->srstate, 8000.0, (double)samplerate);
rc = init_samplerate(&sender->srstate, 8000.0, (double)samplerate, 3300.0);
if (rc < 0) {
PDEBUG(DSENDER, DEBUG_ERROR, "Failed to init sample rate conversion!\n");
goto error;

@ -3,7 +3,7 @@
#include <math.h>
#include <string.h>
#include "../common/sample.h"
#include "../common/filter.h"
#include "../common/iir_filter.h"
#include "../common/emphasis.h"
#include "../common/debug.h"

@ -3,7 +3,7 @@
#include <math.h>
#include <string.h>
#include "../common/sample.h"
#include "../common/filter.h"
#include "../common/iir_filter.h"
#include "../common/debug.h"
#define level2db(level) (20 * log10(level))
@ -36,8 +36,8 @@ static void gen_samples(sample_t *samples, double freq)
int main(void)
{
filter_t filter_low;
filter_t filter_high;
iir_filter_t filter_low;
iir_filter_t filter_high;
sample_t samples[SAMPLERATE];
double level;
int iter = 2;
@ -47,11 +47,11 @@ int main(void)
printf("testing low-pass filter with %d iterations\n", iter);
filter_lowpass_init(&filter_low, 1000.0, SAMPLERATE, iter);
iir_lowpass_init(&filter_low, 1000.0, SAMPLERATE, iter);
for (i = 0; i < 4001; i += 100) {
gen_samples(samples, (double)i);
filter_process(&filter_low, samples, SAMPLERATE);
iir_process(&filter_low, samples, SAMPLERATE);
level = get_level(samples);
printf("%s%4d Hz: %.1f dB", debug_db(level), i, level2db(level));
if (i == 1000)
@ -66,11 +66,11 @@ int main(void)
printf("testing high-pass filter with %d iterations\n", iter);
filter_highpass_init(&filter_high, 2000.0, SAMPLERATE, iter);
iir_highpass_init(&filter_high, 2000.0, SAMPLERATE, iter);
for (i = 0; i < 4001; i += 100) {
gen_samples(samples, (double)i);
filter_process(&filter_high, samples, SAMPLERATE);
iir_process(&filter_high, samples, SAMPLERATE);
level = get_level(samples);
printf("%s%4d Hz: %.1f dB", debug_db(level), i, level2db(level));
if (i == 2000)
@ -85,13 +85,13 @@ int main(void)
printf("testing band-pass filter with %d iterations\n", iter);
filter_lowpass_init(&filter_low, 2000.0, SAMPLERATE, iter);
filter_highpass_init(&filter_high, 1000.0, SAMPLERATE, iter);
iir_lowpass_init(&filter_low, 2000.0, SAMPLERATE, iter);
iir_highpass_init(&filter_high, 1000.0, SAMPLERATE, iter);
for (i = 0; i < 4001; i += 100) {
gen_samples(samples, (double)i);
filter_process(&filter_low, samples, SAMPLERATE);
filter_process(&filter_high, samples, SAMPLERATE);
iir_process(&filter_low, samples, SAMPLERATE);
iir_process(&filter_high, samples, SAMPLERATE);
level = get_level(samples);
printf("%s%4d Hz: %.1f dB", debug_db(level), i, level2db(level));
if (i == 1000)

@ -4,7 +4,7 @@
#include <string.h>
#include <sys/time.h>
#include "../common/sample.h"
#include "../common/filter.h"
#include "../common/iir_filter.h"
#include "../common/fm_modulation.h"
#include "../common/debug.h"
@ -33,7 +33,7 @@ sample_t samples[SAMPLES];
float buff[SAMPLES * 2];
fm_mod_t mod;
fm_demod_t demod;
filter_t lp;
iir_filter_t lp;
int main(void)
{
@ -47,19 +47,19 @@ int main(void)
fm_demodulate(&demod, samples, SAMPLES, buff);
T_STOP("FM demodulate", SAMPLES)
filter_lowpass_init(&lp, 10000.0 / 2.0, 50000, 1);
iir_lowpass_init(&lp, 10000.0 / 2.0, 50000, 1);
T_START()
filter_process(&lp, samples, SAMPLES);
iir_process(&lp, samples, SAMPLES);
T_STOP("low-pass filter (second order)", SAMPLES)
filter_lowpass_init(&lp, 10000.0 / 2.0, 50000, 2);
iir_lowpass_init(&lp, 10000.0 / 2.0, 50000, 2);
T_START()
filter_process(&lp, samples, SAMPLES);
iir_process(&lp, samples, SAMPLES);
T_STOP("low-pass filter (fourth order)", SAMPLES)
filter_lowpass_init(&lp, 10000.0 / 2.0, 50000, 4);
iir_lowpass_init(&lp, 10000.0 / 2.0, 50000, 4);
T_START()
filter_process(&lp, samples, SAMPLES);
iir_process(&lp, samples, SAMPLES);
T_STOP("low-pass filter (eigth order)", SAMPLES)
return 0;

Loading…
Cancel
Save