#ifndef __RX_H
#define __RX_H

#include <stdint.h>
#include <arm_math.h>

// RX ADC
#define RX_ADC_CLOCK               (700000L)       // (168000000/((47,5+12,5)*4)
#define RX_ADC_OVERSAMPLING        (2)
#define RX_ADC_SAMPLE_RATE         (RX_ADC_CLOCK/RX_ADC_OVERSAMPLING)
#define RX_ADC_BUFFER_SIZE         (1024)
#define RX_ADC_SAMPLES_BUFFER_RATE (RX_ADC_SAMPLE_RATE/RX_ADC_BUFFER_SIZE)

// RX DAC
#define RX_DAC_DIVISOR          7680
#define RX_DAC_SAMPLE_RATE              (CLOCK/RX_DAC_DIVISOR) // 21875

// 1° stadio
// decimazione del singolo stadio
#define MS_HBF_TAP_NUM                          (15)
#define MS_DECIMATION_FACTOR                    (2)
#define MS_DECIMATION_FACTOR_MASK               (MS_DECIMATION_FACTOR - 1)
// ringbuf
#define MIXED_SAMPLES_RINGBUFFER_SIZE           (64)    // l'ultimo deve essere grande almeno come MS_HBF_TAP_NUM
#define MIXED_SAMPLES_RINGBUFFER_SIZE_MASK      (MIXED_SAMPLES_RINGBUFFER_SIZE - 1)
#define MIXED_SAMPLES_2M_RINGBUFFER_SIZE        (MIXED_SAMPLES_RINGBUFFER_SIZE / 2)
#define MIXED_SAMPLES_2M_RINGBUFFER_SIZE_MASK   (MIXED_SAMPLES_2M_RINGBUFFER_SIZE - 1)

#define ST1_OUT_SAMPLE_RATE     (RX_ADC_SAMPLE_RATE/(MS_DECIMATION_FACTOR*2))

// 2° stadio
#define ST2_BUFFER_SIZE         (256)
#define ST2_DECIMATION_FACTOR   (4)
#define ST2_OUT_SAMPLE_RATE     (ST1_OUT_SAMPLE_RATE/ST2_DECIMATION_FACTOR)
#define ST2_FILTER_BLOCK_SIZE   (ST2_BUFFER_SIZE)
#define ST2_FILTER_TAP_NUM      (128)

#define RX_DAC_BUFFER_SIZE          (ST2_BUFFER_SIZE/ST2_DECIMATION_FACTOR)

// audio filter
#define RX_AUDIO_FILTER_TAP_NUM    128
#define RX_AUDIO_FILTER_FREQ_DEF   (1250)
#define RX_AUDIO_FILTER_FREQ_MAX   (RX_DAC_SAMPLE_RATE/2)
#define RX_AUDIO_FILTER_BW_DEF     (2000)
#define RX_AUDIO_FILTER_BW_MAX     (RX_DAC_SAMPLE_RATE/2)
#define RX_AUDIO_FILTER_BETA_DEF   (16)
#define RX_AUDIO_FILTER_BLOCK_SIZE RX_DAC_BUFFER_SIZE

// demodulatore
#define RX_NCO_FREQUENCY          (RX_DAC_SAMPLE_RATE/((0xFFFFFFFF)/NCO2_INCREMENT)) // 1750

#define RX_SMETER_SCALE		4

extern uint8_t rx_signal, rx_signal_last;
extern q31_t rx_nco1_increment;

// RX ADC
extern volatile uint8_t rx_adc_buffer_ready;
extern uint16_t rx_adc_buffer[RX_ADC_BUFFER_SIZE];

// RX DAC
extern volatile uint8_t rx_dac_buffer_toggle, rx_dac_buffer_mezzo_vuoto;
extern q31_t prefilter_rx_dac_buffer[RX_DAC_BUFFER_SIZE];
extern q31_t rx_dac_buffer[2][RX_DAC_BUFFER_SIZE];

// filtro st2
extern arm_fir_decimate_instance_q31 st2_filter_I_struct;
extern arm_fir_decimate_instance_q31 st2_filter_Q_struct;
extern q31_t st2_filter_I_state[ST2_FILTER_BLOCK_SIZE + ST2_FILTER_TAP_NUM - 1];
extern q31_t st2_filter_Q_state[ST2_FILTER_BLOCK_SIZE + ST2_FILTER_TAP_NUM - 1];
extern int32_t st2_filter_taps[ST2_FILTER_TAP_NUM];
extern int32_t st2_wide_taps[ST2_FILTER_TAP_NUM];

// filtro audio
extern arm_fir_instance_q31 audio_filter_struct;
extern q31_t audio_filter_state[RX_AUDIO_FILTER_BLOCK_SIZE + RX_AUDIO_FILTER_TAP_NUM - 1];
extern int32_t audio_filter_coeffs[RX_AUDIO_FILTER_TAP_NUM];

// mix
void rx_mixer(uint16_t *in, uint16_t size, q31_t *I_out, q31_t *Q_out, int32_t nco_freq);

// demodulatori
void ssb_demodulator(q31_t *in_I, q31_t *in_Q, uint16_t size, q31_t *out, q31_t nco_freq);
void dc_demodulator(q31_t *in, uint16_t size, q31_t *out);
void am_demodulator(q31_t *in_I, q31_t *in_Q, uint16_t size, q31_t *out);

// filtri
// void filter_init(void);
void set_rx_audio_filter(void);
void st2_filter_init(void);
void rx_inizializza_filtro_audio(void);
void audio_filter_generate_coeffs(int32_t *Coeffs, uint32_t freq, uint32_t bw, uint8_t beta);
q31_t hb_fir15(q31_t * samples_ringbuf, uint8_t sample_index, uint8_t buff_size_mask, q31_t * coefficients);

#endif
