?? experiment-2.cpp
字號:
#include"math.h"
#include"stdio.h"
#include"stdlib.h"
extern void signal(float *h, float f, float dt, float m, int N);
extern void Output_r(float *r, float *y2, int L);
extern void Output_conv(float *y, int L);
void convolution(float *y, float *h, int M, float *x, int N);
void relation(float *r, int M, float *y1, float *y2, int N);
////////////////////////////////////////////////////////
// 試驗-2: 計算序列的卷積和相關函數 //
// //
// 1. 計算序列h(n)和x(n)的卷積,h(n)*x(n)。 //
// //
// 2. 計算序列h(n)和h(n-45)的相關函數。 //
// //
// 3. 用Excel軟件繪圖顯示計算結果。 //
// //
//-------------------------------------------------------------//
// //
// 編寫程序的位置: //
// //
// 1. 在函數void convolution()中完成卷積計算的編程 //
// //
// 3. 在函數relation()中完成相關函數計算的編程 //
// //
/////////////////////////////////////////////////////////////////
void main()
{
float h[128], deltat, M, f;
int N, i;
//-------------------------------------------//
// 生成離散信號 h(n) //
//-------------------------------------------//
deltat=float(0.004);
N=32; // h[n]數據長度(點數)
f=35;
M=float(1.5);
for(i=0; i<N; i++)
{ h[i]=0; }
signal(h, f, deltat, M, N); // 調用函數signal形成離散信號h(n)
//--------------------------------------------------------------//
// 任務1: 計算序列h(n)和x(n)的卷積 //
//--------------------------------------------------------------//
float y[128], x[128]; // y[i]數組存放卷積結果.
// x[i]數組程序內生成.
int L;
L=128; // 離散信號x(n)的長度
//-----------------------------//
// 形成信號 x(n) //
//-----------------------------//
for(i=0; i<L; i++)
{ x[i]=0; }
x[11] =float(1.0);
x[45] =float(-0.6);
x[85] =float(0.8);
//-----------------------------------//
// 計算卷積 y[n]=h[n]*x[n] //
//-----------------------------------//
convolution(y, h, N, x, L);
//-----------------------------//
// 輸出卷積結果 y[n] //
//-----------------------------//
Output_conv(y, L);
//--------------------------------------------------------------//
// 任務2: 計算序列h[n]和h[n-45]的相關函數 //
//--------------------------------------------------------------//
float r[128], y1[128], y2[128]; // y1[i]數組存放h[n];
// y2[i]數組存放h[n-45];
// r[i]數組存放相關函數;
for(i=0; i<L; i++) { y1[i]=y2[i]=r[i]=0; }
for(i=0; i<N; i++) { y1[i]=h[i]; y2[i+45]=h[i]; }
//-----------------------------//
// 計算相關函數 r(n) //
//-----------------------------//
relation(r, L, y1, y2, L);
//------------------------------------//
// 輸出相關函數 r(n) 和 信號 h(n-20) //
//------------------------------------//
Output_r(y1, y2, L);
Output_r(r,y2,L);
//-----------------------------------------//
printf(" End of caculating !!\n\n");
//-----------------------------------------//
}
//####################################################//
// 計算卷積的程序 //
// --------------------------------------------------//
// 在下列函數段中編寫計算函數的程序 !!! //
// --------------------------------------------------//
//####################################################//
void convolution(float *y, float *h, int N, float *x, int L)
{
int n=0,m;
while(n<N+L)
{
y[n]=0;
for(m=0;m<N;m++)
{
if((n-m)>=0&&(n-m)<L)
{
y[n]+=h[m]*x[n-m];
}
}
n++;
}
}
//####################################################//
// 計算相關函數的程序 //
// --------------------------------------------------//
// 在下列函數段中編寫計算相關函數的程序 !!! //
// --------------------------------------------------//
//####################################################//
void relation(float *r, int M, float *y1, float *y2, int L)
{
int n,m;
//for(n=-(L-1);n<L;n++)
for(n=0;n<L;n++)
{
// r[n+L-1]=0.0;
r[n]=0;
// for(m=0;m<L;m++)
for(m=0;m<L;m++)
{
if((m+n)>=0&&(m+n)<L)
// r[n+L-1]+=y1[m]*y2[m+n];
r[n]+=y1[m]*y2[m+n];
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -