cw32L010开发代码单片机解密
cw32L010开发代码单片机解密
void BandpassFilter_UpdateCoeffs(BandpassFilter* filter, float center_freq, float bandwidth) {
-
- float omega;
- float sin_omega;
- float cos_omega;
- float alpha;
- float a0;
- float Q;
- // 参数边界检查
- if (center_freq < 20.0f) center_freq = 20.0f;
- if (center_freq > (SAMPLE_RATE / 2.5f)) center_freq = SAMPLE_RATE / 2.5f;
- if (bandwidth < 10.0f) bandwidth = 10.0f;
- if (bandwidth > center_freq) bandwidth = center_freq;
- filter->center_freq = center_freq;
- filter->bandwidth = bandwidth;
- // 角频率
- omega = 2.0f * 3.14159265f * center_freq / SAMPLE_RATE;
- sin_omega = sin_approx(omega);
- cos_omega = cos_approx(omega);
- // Q值计算: Q = center_freq / bandwidth
- Q = center_freq / bandwidth;
- if (Q > 20.0f) Q = 20.0f; // 限制Q值防止不稳定
- if (Q < 0.5f) Q = 0.5f;
- // 计算alpha
- alpha = sin_omega / (2.0f * Q);
- // 带通滤波器系数 (恒定0dB峰值增益)
- // b0 = alpha
- // b1 = 0
- // b2 = -alpha
- // a0 = 1 + alpha
- // a1 = -2*cos(omega)
- // a2 = 1 - alpha
- a0 = 1.0f + alpha;
- filter->b0 = alpha / a0;
- filter->b1 = 0.0f;
- filter->b2 = -alpha / a0;
- filter->a1 = -2.0f * cos_omega / a0;
- filter->a2 = (1.0f - alpha) / a0;
- }
- /**
- * 初始化滤波器
- */
- void BandpassFilter_Init(BandpassFilter* filter) {
- filter->x1 = 0.0f;
- filter->x2 = 0.0f;
- filter->y1 = 0.0f;
- filter->y2 = 0.0f;
- filter->center_freq = 1000.0f;
- filter->bandwidth = 200.0f;
- // 计算初始系数
- BandpassFilter_UpdateCoeffs(filter, filter->center_freq, filter->bandwidth);
- }
- /**
- * 滤波器处理 (Direct Form II Transposed)
- * @param filter: 滤波器实例指针
- * @param input: 输入样本 (归一化到 -1.0 ~ 1.0)
- * @return: 滤波后输出 (归一化)
- */
- float BandpassFilter_Process(BandpassFilter* filter, float input) {
- // Direct Form II Transposed 结构
- // y = b0*x + s1
- // s1 = b1*x - a1*y + s2
- // s2 = b2*x - a2*y
- float output = filter->b0 * input + filter->y1;
- filter->y1 = filter->b1 * input - filter->a1 * output + filter->y2;
- filter->y2 = filter->b2 * input - filter->a2 * output;
- return output;
- }
- /* ==================== 硬件抽象层 (需根据具体单片机修改) ==================== */
- /**
- * ADC初始化
- * 需要根据具体单片机修改
- */
- void ADC_Init(void) {
-
- ADCCFG = ADCCFG | 0X20;
- ADC_CONTR = 0X80;
- }
- /**
- * 读取ADC值
- * @return: 12位ADC值 (0-4095)
- */
- uint16_t ADC_Read(uint8_t ch) {//P1.0
-
- ADC_RES = 0;
- ADC_RESL = 0;
- ADC_CONTR = (ADC_CONTR & 0XE0) | 0X40 | ch;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while((ADC_CONTR & 0X20) == 0);
- ADC_CONTR &= ~0X20;
- return (((unsigned int)ADC_RES<<8) | ADC_RESL);
- }

芯片解密