📄 dsp自适应滤波器的c语言仿真.txt
字号:
我这边有一个定点DSP自适应滤波器的C语言仿真。我也在做这个课程作业DSP程序正在调试^^
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.14159
#define DELAY 1
#define M0 16
#define MD 17
#define Q 15
short signal_make(short N,short T);
int mpy(int,int);
int mpyh(int,int);
int mpyhl(int,int);
int add2(int,int);
int main()
{
/*
// test
short i = 32767;
short j = -12325;
short z;
int temp;
temp = (int)i;
z = (i * j) >> 15;
printf("%d",z);
*/
short y,e;
short N = 30000;
FILE *fp1;
FILE *fp2;
int S[MD];
int W[M0];
short st;
short i = N;
short u0 = 10;
short u,j;
int sum;
int *p1 = NULL;
int *p2 = NULL;
int a;
signal_make(N,100);
u = u0;
fp1 = fopen("signal_1","rb");
fp2 = fopen("signal_2","wb+");
for (j = M0 - 1;j >= 0;j--)
{
W[j] = 0;
}
for (j = MD - 1;j >= 0;j--)
{
S[j] = 0;
}
while (i)
{
fread(&st,sizeof(short),1,fp1);
// printf("%d\n",st);
p2 = (int*)((short*)S + 1);
for (p1 = S;p1 <= &S[MD - 1];p1++)
{
*p1 = *p2;
p2++;
}
sum = 0;
*((short*)(&S[MD - 1]) + 1)= st;
for (j = 0;j < M0;j++)
{
sum += mpy(S[j],W[j]);
sum += mpyh(S[j],W[j]);
}
y = (short)(sum >> Q);
e = st - y;
a = (u * e) >> (Q - 1);
for (j = 0;j < M0;j++)
{
sum = ((mpy(S[j],a) >> Q) & 0x0000ffff) + ((mpyhl(S[j],a) << (16 - Q)) & 0xffff0000) ;
W[j] = add2(W[j],sum);
}
u = (u0 >> 1) + ((e * e) >> (Q + 11));
fwrite(&y,sizeof(short),1,fp2);
i--;
}
fclose(fp1);
fclose(fp2);
return 0;
}
int add2(int a,int b)
{
short *p1;
short *p2;
short *p3;
int c;
p1 = (short*)(&a);
p2 = (short*)(&b);
p3 = (short*)(&c);
*p3 = *p1 + *p2;
p1++;
p2++;
p3++;
*p3 = *p1 + *p2;
return c;
}
int mpy(int a,int b)
{
short *p1;
short *p2;
int c;
p1 = (short*)(&a);
p2 = (short*)(&b);
c = *p1 * *p2;
return c;
}
int mpyhl(int a,int b)
{
short *p1;
short *p2;
int c;
p1 = (short*)(&a) + 1;
p2 = (short*)(&b);
c = *p1 * *p2;
return c;
}
int mpyh(int a,int b)
{
short *p1;
short *p2;
int c;
p1 = (short*)(&a) + 1;
p2 = (short*)(&b) + 1;
c = *p1 * *p2;
return c;
}
short signal_make(short N,short T)
{
short i = N;
short x,r;
double temp;
FILE *fp;
if ((fp = fopen("signal_1","wb+")) == NULL)
{
printf("can not open file.");
return -1;
}
while (i)
{
temp = sin(2 * PI * i / T);
r = (rand() - 16384) >> 3;
x = (short)(temp * 5000 + r);//**
if ((fwrite(&x,sizeof(short),1,fp)) != 1)
printf("file write error\n");
i--;
}
fclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -