Filters reworked for new samplerate

This commit is contained in:
Mark Qvist 2018-12-30 00:32:19 +01:00
parent 74e0b0d1db
commit 56bed68143
4 changed files with 69 additions and 57 deletions

View File

@ -14,8 +14,6 @@
// Sampling & timer setup // Sampling & timer setup
#define CONFIG_SAMPLERATE 19200UL #define CONFIG_SAMPLERATE 19200UL
//#define CONFIG_SAMPLERATE 19200UL
//#define CONFIG_SAMPLERATE 9600
// Serial settings // Serial settings
#define BAUD 115200 #define BAUD 115200
@ -25,6 +23,9 @@
// CSMA Settings // CSMA Settings
#define CONFIG_CSMA_P 255 #define CONFIG_CSMA_P 255
// Packet settings
#define CONFIG_PASSALL false
// Port settings // Port settings
#if TARGET_CPU == m1284p #if TARGET_CPU == m1284p
#define ADC_PORT PORTA #define ADC_PORT PORTA

View File

@ -375,54 +375,53 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) {
afsk->iirX[0] = afsk->iirX[1]; afsk->iirX[0] = afsk->iirX[1];
#if FILTER_CUTOFF == 600 #if CONFIG_SAMPLERATE == 9600
afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2; #if FILTER_CUTOFF == 600
// The above is a simplification of: afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2;
// afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 3.558147322; // The above is a simplification of:
#elif FILTER_CUTOFF == 800 // afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 3.558147322;
afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2; #else
// The above is a simplification of: #error Unsupported filter cutoff!
// afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 2.899043379; #endif
#elif FILTER_CUTOFF == 1200 #elif CONFIG_SAMPLERATE == 19200
afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 1; #if FILTER_CUTOFF == 600
// The above is a simplification of: afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 6;
// afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 2.228465666; #else
#elif FILTER_CUTOFF == 1600 #error Unsupported filter cutoff!
afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 1; #endif
// The above is a simplification of:
// afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) / 1.881349100;
#else #else
#error Unsupported filter cutoff! #error Unsupported samplerate!
#endif #endif
afsk->iirY[0] = afsk->iirY[1]; afsk->iirY[0] = afsk->iirY[1];
#if FILTER_CUTOFF == 600 #if CONFIG_SAMPLERATE == 9600
afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] >> 1); #if FILTER_CUTOFF == 600
// The above is a simplification of a first-order 600Hz chebyshev filter: afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] >> 1);
// afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.4379097269); // The above is a simplification of:
#elif FILTER_CUTOFF == 800 // afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.4379097269);
afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] / 3); #else
// The above is a simplification of a first-order 800Hz chebyshev filter: #error Unsupported filter cutoff!
// afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.3101172565); #endif
#elif FILTER_CUTOFF == 1200 #elif CONFIG_SAMPLERATE == 19200
afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] / 10); #if FILTER_CUTOFF == 600
// The above is a simplification of a first-order 1200Hz chebyshev filter: afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] / 2);
// afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * 0.1025215106); #else
#elif FILTER_CUTOFF == 1600 #error Unsupported filter cutoff!
afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + -1*(afsk->iirY[0] / 17); #endif
// The above is a simplification of a first-order 1600Hz chebyshev filter:
// afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] * -0.0630669239);
#else #else
#error Unsupported filter cutoff! #error Unsupported samplerate!
#endif #endif
//int8_t freq_disc = (int8_t)fifo_pop(&afsk->delayFifo) * currentSample;
// We put the sampled bit in a delay-line: // We put the sampled bit in a delay-line:
// First we bitshift everything 1 left // First we bitshift everything 1 left
afsk->sampledBits <<= 1; afsk->sampledBits <<= 1;
// And then add the sampled bit to our delay line // And then add the sampled bit to our delay line
afsk->sampledBits |= (afsk->iirY[1] > 0) ? 0 : 1; afsk->sampledBits |= (afsk->iirY[1] > 0) ? 0 : 1;
//afsk->sampledBits |= (freq_disc > 0) ? 0 : 1;
// Put the current raw sample in the delay FIFO // Put the current raw sample in the delay FIFO
fifo_push(&afsk->delayFifo, currentSample); fifo_push(&afsk->delayFifo, currentSample);
@ -489,6 +488,7 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) {
// the last 3 sampled bits. If there is two or // the last 3 sampled bits. If there is two or
// more 1's, we will assume that the transmitter // more 1's, we will assume that the transmitter
// sent us a one, otherwise we assume a zero // sent us a one, otherwise we assume a zero
uint8_t bits = afsk->sampledBits & 0x07; uint8_t bits = afsk->sampledBits & 0x07;
if (bits == 0x07 || // 111 if (bits == 0x07 || // 111
bits == 0x06 || // 110 bits == 0x06 || // 110
@ -498,15 +498,17 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) {
afsk->actualBits |= 1; afsk->actualBits |= 1;
} }
//// Alternative using five bits ////////////////
// uint8_t bits = afsk->sampledBits & 0x0f; //// Alternative using six bits ////////////////
// uint8_t c = 0; // uint8_t bits = afsk->sampledBits & 0x3F;
// c += bits & BV(1); // uint8_t c = 0;
// c += bits & BV(2); // c += bits & _BV(0);
// c += bits & BV(3); // c += bits & _BV(1);
// c += bits & BV(4); // c += bits & _BV(2);
// c += bits & BV(5); // c += bits & _BV(3);
// if (c >= 3) afsk->actualBits |= 1; // c += bits & _BV(4);
// c += bits & _BV(5);
// if (c >= 3) afsk->actualBits |= 1;
///////////////////////////////////////////////// /////////////////////////////////////////////////
// Now we can pass the actual bit to the HDLC parser. // Now we can pass the actual bit to the HDLC parser.

View File

@ -35,9 +35,11 @@ inline static uint8_t sinSample(uint16_t i) {
#define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01) #define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01)
#define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1) #define TRANSITION_FOUND(bits) BITS_DIFFER((bits), (bits) >> 1)
// TODO: Maybe expand number of bits looked at here: // TODO: Maybe revert to only looking at two samples
#define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03) #define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03)
#define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2) #define QUAD_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x0F) == 0x0F)
#define SIGNAL_TRANSITIONED(bits) QUAD_XOR((bits), (bits) >> 4)
// #define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2)
#define CPU_FREQ F_CPU #define CPU_FREQ F_CPU
@ -53,16 +55,19 @@ inline static uint8_t sinSample(uint16_t i) {
#define SAMPLESPERBIT (CONFIG_SAMPLERATE / BITRATE) #define SAMPLESPERBIT (CONFIG_SAMPLERATE / BITRATE)
#define TICKS_BETWEEN_SAMPLES ((((CPU_FREQ+FREQUENCY_CORRECTION)) / CONFIG_SAMPLERATE) - 1) #define TICKS_BETWEEN_SAMPLES ((((CPU_FREQ+FREQUENCY_CORRECTION)) / CONFIG_SAMPLERATE) - 1)
// TODO: Calculate based on sample rate // TODO: Calculate based on sample rate [Done?]
#define PHASE_INC SAMPLESPERBIT/8 // Nudge by an eigth of a sample each adjustment #define PHASE_BITS 8 // 8 // Sub-sample phase counter resolution
#define PHASE_BITS 8 // How much to increment phase counter each sample #define PHASE_INC 1 // 1 // Nudge by above resolution for each adjustment
#define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // Resolution of our phase counter #define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // 128 // Size of our phase counter
#define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window // TODO: Test which target is best in real world
#define PHASE_THRESHOLD (PHASE_MAX / 2)+3*PHASE_BITS // Target transition point of our phase window
//#define PHASE_THRESHOLD (PHASE_MAX / 2) // 64 // Target transition point of our phase window
#define DCD_TIMEOUT_SAMPLES CONFIG_SAMPLERATE/100 #define DCD_TIMEOUT_SAMPLES CONFIG_SAMPLERATE/100
#define DCD_MIN_COUNT CONFIG_SAMPLERATE/1600 #define DCD_MIN_COUNT CONFIG_SAMPLERATE/1600
// TODO: Revamp filtering
#if BITRATE == 1200 #if BITRATE == 1200
#define FILTER_CUTOFF 600 #define FILTER_CUTOFF 600
#define MARK_FREQ 1200 #define MARK_FREQ 1200
@ -120,8 +125,12 @@ typedef struct Afsk
int16_t iirX[2]; // IIR Filter X cells int16_t iirX[2]; // IIR Filter X cells
int16_t iirY[2]; // IIR Filter Y cells int16_t iirY[2]; // IIR Filter Y cells
uint8_t sampledBits; // Bits sampled by the demodulator (at ADC speed) #if SAMPLESPERBIT < 17
int8_t currentPhase; // Current phase of the demodulator uint16_t sampledBits; // Bits sampled by the demodulator (at ADC speed)
#else
#error Not enough space in sampledBits variable!
#endif
int16_t currentPhase; // Current phase of the demodulator
uint8_t actualBits; // Actual found bits at correct bitrate uint8_t actualBits; // Actual found bits at correct bitrate
volatile int status; // Status of the modem, 0 means OK volatile int status; // Status of the modem, 0 means OK

View File

@ -31,7 +31,7 @@ void ax25_poll(AX25Ctx *ctx) {
while ((c = fgetc(ctx->ch)) != EOF) { while ((c = fgetc(ctx->ch)) != EOF) {
if (!ctx->escape && c == HDLC_FLAG) { if (!ctx->escape && c == HDLC_FLAG) {
if (ctx->frame_len >= AX25_MIN_FRAME_LEN) { if (ctx->frame_len >= AX25_MIN_FRAME_LEN) {
if (ctx->crc_in == AX25_CRC_CORRECT) { if (ctx->crc_in == AX25_CRC_CORRECT || CONFIG_PASSALL) {
#if OPEN_SQUELCH == true #if OPEN_SQUELCH == true
LED_RX_ON(); LED_RX_ON();
#endif #endif