experiment with the pll

This commit is contained in:
2025-03-23 17:38:41 +01:00
parent 52e081ad3f
commit ecbeb6a18f
3 changed files with 10 additions and 7 deletions
+5 -2
View File
@@ -20,17 +20,18 @@ void init_pll(PLL *pll, float freq, float loop_filter_bandwidth, int quadrature_
pll->loop_filter_state = 0.0f;
pll->kp = M_2PI * loop_filter_bandwidth;
pll->ki = 0.25f * pll->kp * pll->kp;
pll->last_output = 0.0f;
pll->sample_rate = sample_rate;
pll->quadrature_mode = quadrature_mode;
}
float apply_pll(PLL *pll, float ref_sample, float input_sample) {
float apply_pll(PLL *pll, float ref_sample) {
float phase_error;
float vco_output = sinf(pll->phase);
if (pll->quadrature_mode) vco_output = sinf(pll->phase + (M_PI / 2.0f)); // 90 degrees
phase_error = ref_sample * input_sample;
phase_error = ref_sample * pll->last_output;
pll->loop_filter_state += pll->ki * phase_error / pll->sample_rate;
float loop_output = pll->loop_filter_state + pll->kp * phase_error;
@@ -47,5 +48,7 @@ float apply_pll(PLL *pll, float ref_sample, float input_sample) {
pll->phase += M_2PI;
}
pll->last_output = vco_output;
return vco_output;
}
+2 -1
View File
@@ -22,8 +22,9 @@ typedef struct {
float loop_filter_state;
float kp;
float ki;
float last_output;
int sample_rate;
int quadrature_mode;
} PLL;
void init_pll(PLL *pll, float freq, float loop_filter_bandwidth, int quadrature_mode, int sample_rate);
float apply_pll(PLL *pll, float ref_sample, float input_sample);
float apply_pll(PLL *pll, float ref_sample);