您的位置:

滤波c语言代码,滤波器c语言实现

本文目录一览:

求:一个关于FIR带通滤波器的C语言设计程序 代码

short h[], short y[])

{

int i, j, sum; for (j = 0; j 100; j++) {

sum = 0;

for (i = 0; i 32; i++)

sum += x[i+j] * h[i];

y[j] = sum 15;

}

}

2

void fir(short x[], short h[], short y[])

{

int i, j, sum0, sum1;

short x0,x1,h0,h1; for (j = 0; j 100; j+=2) {

sum0 = 0;

sum1 = 0;

x0 = x[j];

for (i = 0; i 32; i+=2){

x1 = x[j+i+1];

h0 = h[i];

sum0 += x0 * h0;

sum1 += x1 * h0;

x0 = x[j+i+2];

h1 = h[i+1];

sum0 += x1 * h1;

sum1 += x0 * h1;

}

y[j] = sum0 15;

y[j+1] = sum1 15;

}

}

3

void fir(short x[], short h[], short y[])

{

int i, j, sum0, sum1;

short x0,x1,x2,x3,x4,x5,x6,x7,h0,h1,h2,h3,h4,h5,h6,h7; for (j = 0; j 100; j+=2) {

sum0 = 0;

sum1 = 0;

x0 = x[j];

for (i = 0; i 32; i+=8){

x1 = x[j+i+1];

h0 = h[i];

sum0 += x0 * h0;

sum1 += x1 * h0;

x2 = x[j+i+2];

h1 = h[i+1];

sum0 += x1 * h1;

sum1 += x2 * h1;

x3 = x[j+i+3];

h2 = h[i+2];

sum0 += x2 * h2;

sum1 += x3 * h2;

x4 = x[j+i+4];

h3 = h[i+3];

sum0 += x3 * h3;

sum1 += x4 * h3;

x5 = x[j+i+5];

h4 = h[i+4];

sum0 += x4 * h4;

sum1 += x5 * h4;

x6 = x[j+i+6];

h5 = h[i+5];

sum0 += x5 * h5;

sum1 += x6 * h5;

x7 = x[j+i+7];

h6 = h[i+6];

sum0 += x6 * h6;

sum1 += x7 * h6;

x0 = x[j+i+8];

h7 = h[i+7];

sum0 += x7 * h7;

sum1 += x0 * h7;

}

y[j] = sum0 15;

y[j+1] = sum1 15;

}

}

一段matlab低通滤波器程序,求改编成C语言。

这个我刚好做过一个滤波器,事实上对时域信号做FFT,截取一定点数再做逆FFT相当于理想滤波。设计滤波器代码如下:

f1=100;f2=200;%待滤波正弦信号频率

fs=2000;%采样频率

m=(0.3*f1)/(fs/2);%定义过度带宽

M=round(8/m);%定义窗函数的长度

N=M-1;%定义滤波器的阶数

b=fir1(N,f2/fs);%使用fir1函数设计滤波器

%输入的参数分别是滤波器的阶数和截止频率

figure(1)

[h,f]=freqz(b,1,512);%滤波器的幅频特性图

%[H,W]=freqz(B,A,N)当N是一个整数时函数返回N点的频率向量和幅频响应向量

plot(f*fs/(2*pi),20*log10(abs(h)))%参数分别是频率与幅值

xlabel('频率/赫兹');ylabel('增益/分贝');title('滤波器的增益响应');

figure(2)

subplot(211)

t=0:1/fs:0.5;%定义时间范围和步长

s=sin(2*pi*f1*t)+sin(2*pi*f2*t);%滤波前信号

plot(t,s);%滤波前的信号图像

xlabel('时间/秒');ylabel('幅度');title('信号滤波前时域图');

subplot(212)

Fs=fft(s,512);%将信号变换到频域

AFs=abs(Fs);%信号频域图的幅值

f=(0:255)*fs/512;%频率采样

plot(f,AFs(1:256));%滤波前的信号频域图

xlabel('频率/赫兹');ylabel('幅度');title('信号滤波前频域图');

figure(3)

sf=filter(b,1,s);%使用filter函数对信号进行滤波

%参数分别为滤波器系统函数的分子和分母多项式系数向量和待滤波信号输入

subplot(211)

plot(t,sf)%滤波后的信号图像

xlabel('时间/秒');ylabel('幅度');title('信号滤波后时域图');

axis([0.2 0.5 -2 2]);%限定图像坐标范围

subplot(212)

Fsf=fft(sf,512);%滤波后的信号频域图

AFsf=abs(Fsf);%信号频域图的幅值

f=(0:255)*fs/512;%频率采样

plot(f,AFsf(1:256))%滤波后的信号频域图

xlabel('频率/赫兹');ylabel('幅度');title('信号滤波后频域图');

二阶滤波器用C语言怎么写

这个可比你想象的复杂多了,s是个复变量,1/(s+1)极点在-1,要想用C语言写,必须理解清楚下面几个问题:

1、输入必须是个有限序列,比如(x+yi),x和y分别是两个长度为N的数组

2、要过滤的频率,必须是个整型值,或者是个整型区间

3、输出结果同样是两个长度为N的数组(p+qi)

4、整个程序需要使用最基本的复数运算,这一点C语言本身不提供,必须手工写复函数运算库

5、实现的时候具体算法还需要编,这里才是你问题的核心。

我可以送你一段FFT的程序,自己琢磨吧,和MATLAB的概念差别很大:

#include assert.h

#include math.h

#include stdio.h

#include stdlib.h

#include string.h

#include windows.h

#include "complex.h"

extern "C" {

// Discrete Fourier Transform (Basic Version, Without Any Enhancement)

// return - Without Special Meaning, constantly, zero

int DFT (long count, CComplex * input, CComplex * output)

{

assert(count);

assert(input);

assert(output);

CComplex F, X, T, W; int n, i;

long N = abs(count); long Inversing = count 0? 1: -1;

for(n = 0; n N ; n++){ // compute from line 0 to N-1

F = CComplex(0.0f, 0.0f); // clear a line

for(i = 0; i N; i++) {

T = input[i];

W = HarmonicPI2(Inversing * n * i, N);

X = T * W;

F += X; // fininshing a line

}//next i

// save data to outpus

memcpy(output + n, F, sizeof(F));

}//next n

return 0;

}//end DFT

int fft (long count, CComplex * input, CComplex * output)

{

assert(count);

assert(input);

assert(output);

int N = abs(count); long Inversing = count 0? -1: 1;

if (N % 2 || N 5) return DFT(count, input, output);

long N2 = N / 2;

CComplex * iEven = new CComplex[N2]; memset(iEven, 0, sizeof(CComplex) * N2);

CComplex * oEven = new CComplex[N2]; memset(oEven, 0, sizeof(CComplex) * N2);

CComplex * iOdd = new CComplex[N2]; memset(iOdd , 0, sizeof(CComplex) * N2);

CComplex * oOdd = new CComplex[N2]; memset(oOdd , 0, sizeof(CComplex) * N2);

int i = 0; CComplex W;

for(i = 0; i N2; i++) {

iEven[i] = input[i * 2];

iOdd [i] = input[i * 2 + 1];

}//next i

fft(N2 * Inversing, iEven, oEven);

fft(N2 * Inversing, iOdd, oOdd );

for(i = 0; i N2; i++) {

W = HarmonicPI2(Inversing * (- i), N);

output[i] = oEven[i] + W * oOdd[i];

output[i + N2] = oEven[i] - W * oOdd[i];

}//next i

return 0;

}//end FFT

void __stdcall FFT(

long N, // Serial Length, N 0 for DFT, N 0 for iDFT - inversed Discrete Fourier Transform

double * inputReal, double * inputImaginary, // inputs

double * AmplitudeFrequences, double * PhaseFrequences) // outputs

{

if (N == 0) return;

if (!inputReal !inputImaginary) return;

short n = abs(N);

CComplex * input = new CComplex[n]; memset(input, 0, sizeof(CComplex) * n);

CComplex * output= new CComplex[n]; memset(output,0, sizeof(CComplex) * n);

double rl = 0.0f, im = 0.0f; int i = 0;

for (i = 0; i n; i++) {

rl = 0.0f; im = 0.0f;

if (inputReal) rl = inputReal[i];

if (inputImaginary) im = inputImaginary[i];

input[i] = CComplex(rl, im);

}//next i

int f = fft(N, input, output);

double factor = n;

//factor = sqrt(factor);

if (N 0)

factor = 1.0f;

else

factor = 1.0f / factor;

//end if

for (i = 0; i n; i++) {

if (AmplitudeFrequences) AmplitudeFrequences[i] = output[i].getReal() * factor;

if (PhaseFrequences) PhaseFrequences[i] = output[i].getImaginary() * factor;

}//next i

delete [] output;

delete [] input;

return ;

}//end FFT

int __cdecl main(int argc, char * argv[])

{

fprintf(stderr, "%s usage:\n", argv[0]);

fprintf(stderr, "Public Declare Sub FFT Lib \"wfft.exe\" \

(ByVal N As Long, ByRef inputReal As Double, ByRef inputImaginary As Double, \

ByRef freqAmplitude As Double, ByRef freqPhase As Double)");

return 0;

}//end main

};//end extern "C"

c语言中值滤波问题?

1. 是规定做中值滤波的点不含边缘的点(取决于中值滤波窗口大小)。 2,对图像边缘部分的信息进行镜像处理。

如何用C语言实现低通滤波器

float middle_filter(float middle_value [] , intcount)

{

    float sample_value, data;

    int i, j;

    for (i=1; i for(j=count-1; j=i,--j){

        if(middle_value[j-1]=middle_value[j]{

            data=middle_value[j-1];

            middle_value[j-1]=middle_value[j]

            middle_value[j]=data;

        }

    }

    sample_value=middle_value(count-1)/2];

    return(sample_value);

}

帮帮忙,能不能给我 基于C语言的FIR滤波器设计的程序代码(包括CMD,C,ASM),谢谢了 真的很急!!!

#include"math.h"

void firwin(n,band,fln,fhn,wn,h)

int n,band,wn;

double fln,fhn,h[];

{int i,n2,mid;

double s,pi,wc1,wc2,beta,delay;

double window();

beta=0.0;

if(wn==7)

{printf("input beta parameter of Kaiser window(2beta10)\n");

scanf("%1f",beta);

}

pi=4.0*atan(1.0);

if((n%2)==0)/*如果n是偶数*/

{n2=n/2+1;/*这行什么意思*/

mid=1;

}

else

{n2=n/2;

mid=0;

}

delay=n/2.0;

wc1=2.0*pi*fln;

if(band=3) wc2=2.0*pi*fhn;/*先判断用户输入的数据,如果band参数大于3*/

switch(band)

{case 1:

{for(i=0;i=n2;i++)

{s=i-delay;

h[i]=(sin(wc1*s)/(pi*s))*window(wn,n+1,i,beta);

h[n-i]=h[i];

}

if(mid==1) h[n/2]=wc1/pi;

break;

}

case 2:

{for(i=0;i=n2;i++)

{s=i-delay;

h[i]=(sin(pi*s)-sin(wc1*s))/(pi*s);

h[i]=h[i]*window(wn,n+1,i,beta);

h[n-i]=h[i];

}

if(mid==1) h[n/2]=1.0-wc1/pi;

break;

}

case 3:

{for(i=0;in2;i++)

{s=i-delay;

h[i]=(sin(wc2*s)-sin(wc1*s))/(pi*s);

h[i]=h[i]*window(wn,n+1,i,beta);

h[n-i]=h[i];

}

if(mid==1)h[n/2]=(wc2-wc1)/pi;

break;

}

case 4:

{for(i=0;i=n2;i++)

{s=i-delay;

h[i]=(sin(wc1*s)+sin(pi*s)-sin(wc2*s))/(pi*s);

h[i]=h[i]*window(wn,n+1,i,beta);

h[n-i]=h[i];

}

if(mid==1)h[n/2]=(wc1+pi-wc2)/pi;

break;

}

}

}

static double window(type,n,i,beta)

int i,n,type;

double beta;

{int k;

double pi,w;

double kaiser();

pi=4.0*atan(1.0);

w=1.0;

switch(type)

{case 1:

{w=1.0;

break;

}

case 2:

{k=(n-2)/10;

if(i=k)

w=0.5*(1.0-cos(i*pi/(k+1)));

break;

}

case 3:

{w=1.0-fabs(1.0-2*i/(n-1.0));

break;

}

case 4:

{w=0.5*(1.0-cos(2*i*pi/(n-1)));

break;

}

case 5:

{w=0.54-0.46*cos(2*i*pi/(n-1));

break;

}

case 6:

{w=0.42-0.5*cos(2*i*pi/(n-1))+0.08*cos(4*i*pi/(n-1));

break;

}

case 7:

{w=kaiser(i,n,beta);

break;

}

}

return(w);

}

static double kaiser(i,n,beta)

int i,n;

double beta;

{

double a,w,a2,b1,b2,beta1;

double bessel0();

b1=bessel0(beta);

a=2.0*i/(double)(n-1)-1.0;

a2=a*a;

beta1=beta*sqrt(1.0-a2);

b2=bessel0(beta1);

w=b2/b1;

return(w);

}

static double bessel0(x)

double x;

{int i;

double d,y,d2,sum;

y=x/2.0;

d=1.0;

sum=1.0;

for(i=1;i=25;i++)

{d=d*y/i;

d2=d*d;

sum=sum+d2;

if(d2sum*(1.0e-8)) break;

}

return(sum);

}

这是窗函数法的,当然还有其他的比如切比雪夫,零相位滤波什么的,我也在研究,不是很懂哈