CMSIS FIR带通滤波器

时间:2016-07-08 10:28:22

标签: c matlab filter arm signal-processing

我正在尝试在STM32F407微控制器上实现60kHz带通滤波器,并且我遇到了一些问题。我在MATLABs fdatool的帮助下生成了滤波器,然后在MATLAB中进行了仿真。以下MATLAB脚本对其进行了模拟。

% FIR Window Bandpass filter designed using the FIR1 function.
% All frequency values are in Hz.
Fs = 5250000;    % Sampling Frequency

N    = 1800;     % Order
Fc1  = 59950;    % First Cutoff Frequency
Fc2  = 60050;    % Second Cutoff Frequency
flag = 'scale';  % Sampling Flag
% Create the window vector for the design algorithm.
win = hamming(N+1);

% Calculate the coefficients using the FIR1 function.
b  = fir1(N, [Fc1 Fc2]/(Fs/2), 'bandpass', win, flag);
Hd = dfilt.dffir(b);
%----------------------------------------------------------
%----------------------------------------------------------
T = 1 / Fs;          % sample time
L = 4500;            % Length of signal
t = (0:L-1)*T;       % Time vector

% Animate the passband frequency span
for f=55500:50:63500
    signal = sin(2*pi*f*t);
    plot(filter(Hd, signal));
    axis([0 L -1 1]);

    str=sprintf('Signal frequency (Hz) %d', f);
    title(str);
    drawnow;
end

pause;
close all;

signal = sin(2*pi*50000*t) + sin(2*pi*60000*t) + sin(2*pi*78000*t);
signal = signal / 3;
signal = signal(1:1:4500);

filterInput = signal;
filterOutput = filter(Hd,signal);

subplot(2,1,1);
plot(filterInput);
axis([0 4500 -1 1]);

subplot(2,1,2);
plot(filterOutput)
axis([0 4500 -1 1]);
pause;

close all;

从fdatool我以q15格式提取滤波器co-efficents为16位无符号整数,这是因为我使用的是12位ADC。由MATLAB生成的过滤器co-efficents标头为here,并且可以在下面的图片Filter co-efficents

中看到合作效率的结果图。

下面是过滤器实现的代码,显然不起作用,我真的不知道我能做些什么不同,我已经在网上看了一些例子Example 1Example 2

#include "fdacoefs.h"

#define FILTER_SAMPLES 4500
#define BLOCK_SIZE     900    

static uint16_t firInput[FILTER_SAMPLES];
static uint16_t firOutput[FILTER_SAMPLES];
static uint16_t firState[NUM_TAPS + BLOCK_SIZE - 1];

uint16_t util_calculate_filter(uint16_t *buffer, uint32_t len)
{
    uint16_t i;   
    uint16_t max;
    uint16_t min;
    uint32_t index;

    // Create filter instance
    arm_fir_instance_q15 instance; 

    // Ensure that the buffer length isn't longer than the sample size
    if (len > FILTER_SAMPLES)
        len = FILTER_SAMPLES;   

    for (i = 0; i < len ; i++) 
    {
        firInput[i] = buffer[i];        
    }

    // Call Initialization function for the filter 
    arm_fir_init_q15(&instance, NUM_TAPS, &firCoeffs, &firState, BLOCK_SIZE);

    // Call the FIR process function, num of blocks to process = (FILTER_SAMPLES / BLOCK_SIZE)
    for (i = 0; i < (FILTER_SAMPLES / BLOCK_SIZE); i++) // 
    {
        // BLOCK_SIZE = samples to process per call
        arm_fir_q15(&instance, &firInput[i * BLOCK_SIZE], &firOutput[i * BLOCK_SIZE], BLOCK_SIZE); 
    }

    arm_max_q15(&firOutput, len, &max, &index);    
    arm_min_q15(&firOutput, len, &min, &index);

    // Convert output back to uint16 for plotting
    for (i = 0; i < (len); i++) 
    {
        buffer[i] = (uint16_t)(firOutput[i] - 30967);
    }

    return (uint16_t)((max+min));
}

ADC采样速率为5.25 MSPS,它对60kHz信号采样4500次,在这里您可以看到滤波器的Input,然后滤波器的Output非常奇怪。

有什么明显的我错过了吗?因为我完全迷失了,任何指针和提示都有帮助!

1 个答案:

答案 0 :(得分:0)

正如Lundin指出的那样,我改为使用32位整数,这实际上解决了我的问题。当然,我使用MATLABS fdatool生成了新的过滤器协同效应,而不是签名的32位整数。

var item = context.Items.FirstOrDefault();// Database query will be run for this
var start = item.Promotions.FirstOrDefault().Start; // Another query will be run for this
var end = item.Promotions.FirstOrDefault().End; //Will another query be run again? Same query is run again for this
var price = item.Promotions.FirstOrDefault().Price; //Here also