这个可比你想象的复杂多了,s是个复变量,1/(s+1)极点在-1,要想用C语言写,必须理解清楚下面几个问题:
1、输入必须是个有限序列,比如(x+yi),x和y分别是两个长度为N的数组
2、要过滤的频率,必须是个整型值,或者是个整型区间
3、输出结果同样是两个长度为N的数组(p+qi)
4、整个程序需要使用最基本的复数运算,这一点C语言本身不提供,必须手工写复函数运算库
5、实现的时候具体算法还需要编,这里才是你问题的核心。
我可以送你一段FFT的程序,自己琢磨吧,和MATLAB的概念差别很大:
#include <asserth>
#include <mathh>
#include <stdioh>
#include <stdlibh>
#include <stringh>
#include <windowsh>
#include "complexh"
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(00f, 00f); // 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 = 00f, im = 00f; int i = 0;
for (i = 0; i < n; i++) {
rl = 00f; im = 00f;
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 = 10f;
else
factor = 10f / 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 \"wfftexe\" \
(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"
你要给出采样频率,别人才可以给你设计出来的。
=================================
你给的要求太少,但是怎么说也能设计了,先给你个吧。
==================
你可以自己使用fdatool去弄
以下一个结果:通带 5Hz,1db, 阻带8Hz,40db
b=[00117 -00329 00451 -00329 00117]
a=[10000 -36048 49787 -31156 07447]
int filterBegin=5;
double xBuf[5];
double yBuf[5];
double filter(double x)
{
for(int i=4;i>0;i--)
{
yBuf[i] = yBuf[i-1]; xBuf[i] = xBuf[i-1];
}
xBuf[0] = x;
if(filterBegin>0)
{
filterBegin =0;
yBuf[0] = x;
return x;
}
yBuf[0] = 00117 ( xBuf[0] +xBuf[4]) -00329 (xBuf[1 ]+xBuf[3]) +00451xBuf[2 ]
-( -36048 yBuf[1] + 49787yBuf[2] -31156yBuf[3] + 07447yBuf[4]);
return yBuf[0];
}
说的很对,滤波玩的就是增益(衰减)变化,不同的频率,不同的增益(衰减)。称幅频曲线。 1、巴特奥斯滤波器的截止频率指-3dB通频带频率,也就是在这个频率以内保证畅通(通带)。 2、另一个指标叫做阻带,频率大于此值能够保证衰减大于某值
以上就是关于二阶滤波器用C语言怎么写全部的内容,包括:二阶滤波器用C语言怎么写、急求MATLAB低通滤波器 并转换为C语言、如何用c语言实现截止频率为200hz的巴特沃斯低通滤波器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)