?? main.cpp
字號:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "channel.h"
#include "Jakes.h"
/*
*******************************************************************************
* constants and define declarations
*******************************************************************************
*/
/*衰落信道仿真常量定義*/
#define TEST_TIME 100
#define SLOT_LEN 1024
#define SAMPLE_NUM (TEST_TIME*SLOT_LEN)
#define PI 3.141592653589793
/*
********************************************************************************
測試主程序
*
********************************************************************************
*/
void main(void)
{
int i = 0,j = 0,k = 0; /*循環變量*/
/****************************************************************
高斯隨機變量產生用到的變量
****************************************************************/
FILE *fp_random_u = NULL;
FILE *fp_gauss = NULL;
struct awgn gauss;
struct seed awgn_seed = {30323,173,13}; /*白高斯噪聲的隨機數種子*/
struct seed random_u_seed = {100,100,100}; /*均勻分布隨機變量的隨機數種子*/
double ru; /*生成的均勻隨機變量*/
double rg; /*生成的高斯隨機變量*/
double ave; /*高斯隨機變量的均值*/
double sigma; /*高斯隨機變量的標準差*/
/****************************************************************
衰落信道產生用到的變量
****************************************************************/
struct awgn s_gauss;
struct seed s_awgn_seed = {30323,173,13}; /*白高斯信源的隨機數種子*/
int test_cnt;
double tx_slot_i[SLOT_LEN]; /*I路發送信號*/
double tx_slot_q[SLOT_LEN]; /*Q路發送信號*/
double rx_slot_i[SLOT_LEN]; /*I路接收信號*/
double rx_slot_q[SLOT_LEN]; /*Q路接收信號*/
double tx_energy; /*平均發送能量*/
double rx_energy; /*平均接收能量*/
double envelope_pdf[40]; /*統計接收信號幅度的概率密度函數*/
double phase_pdf[40]; /*統計接收信號相位的概率密度函數*/
double envelope;
double phase;
FILE *fp_multipath; /*輸出文件指針,幅度和相位頻率統計*/
FILE *fp_i; /*輸出文件指針,I路輸出*/
FILE *fp_q; /*輸出文件指針,Q路輸出*/
/************************************************************************/
/* 測試AWGN:1.1 均勻分布隨機變量 */
/************************************************************************/
if ((fp_random_u = fopen("output\\random_u.txt","w")) == NULL)
{
printf("can't open file random_u.txt\n");
exit(0);
}
for (i=0; i<1000; i++)
{
ru = random_u(&random_u_seed);
fprintf(fp_random_u,"%f\n",ru);
}
fclose(fp_random_u);
/************************************************************************/
/* 測試AWGN:1.1 高斯隨機變量 */
/************************************************************************/
if ((fp_gauss = fopen("output\\gauss.txt","w")) == NULL)
{
printf("can't open file gauss.txt\n");
exit(0);
}
ave = 0;
sigma = 1.0;//生成零均值,標準差為1的高斯隨機變量
initial_gauss(&gauss, &awgn_seed, ave, sigma);
for (i=0; i<1000; i++)
{
rg = gauss_g(&gauss);
fprintf(fp_gauss,"%f\n",rg);
}
fclose(fp_gauss);
/************************************************************************/
/* 測試衰落信道:2 Jakes模型衰落信道 */
/************************************************************************/
if ((fp_multipath = fopen("output\\multipath.txt","w")) == NULL)
{
printf("can't open file multipath.txt\n");
exit(0);
}
if ((fp_i = fopen("output\\multipath_i.txt","w")) == NULL)
{
printf("can't open file multipath.txt\n");
exit(0);
}
if ((fp_q = fopen("output\\multipath_q.txt","w")) == NULL)
{
printf("can't open file multipath.txt\n");
exit(0);
}
/*用高斯隨機變量做信源,這里參數為均值0,方差0.5*/
initial_gauss(&s_gauss, &s_awgn_seed, 0, 0.71);
/* 設置多徑信道 */
SetMultiPath();
/*變量初始化*/
for (i=0; i<40; i++)
{
envelope_pdf[i] = 0;
phase_pdf[i] = 0;
}
tx_energy = 0;
rx_energy = 0;
for (test_cnt=0; test_cnt<TEST_TIME; test_cnt++ )
{
for (i=0; i<SLOT_LEN; i++)
{
tx_slot_i[i] = gauss_g(&s_gauss);
tx_slot_q[i] = gauss_g(&s_gauss);
}
for (i=0; i<SLOT_LEN; i++)
{
RunMultiPath(tx_slot_i[i], tx_slot_q[i], &rx_slot_i[i], &rx_slot_q[i]);
fprintf(fp_i,"%f\n",rx_slot_i[i]);
fprintf(fp_q,"%f\n",rx_slot_q[i]);
}
for (i=0; i<SLOT_LEN; i++)
{
tx_energy += tx_slot_i[i] * tx_slot_i[i] + tx_slot_q[i] * tx_slot_q[i];
rx_energy += rx_slot_i[i] * rx_slot_i[i] + rx_slot_q[i] * rx_slot_q[i];
envelope = sqrt(rx_slot_i[i] * rx_slot_i[i] + rx_slot_q[i] * rx_slot_q[i]);
phase = atan2(rx_slot_q[i], rx_slot_i[i]) / PI;
/*統計包絡的概率密度函數*/
if (envelope < 0)
{
envelope_pdf[0] += 1;
}
else if (envelope > 4)
{
envelope_pdf[39] += 1;
}
else
{
j = (int)(10*envelope);
envelope_pdf[j] += 1;
}
/*統計相位的概率密度函數*/
if (phase < -1)
{
phase_pdf[0] += 1;
}
else if (phase > 1)
{
phase_pdf[39] += 1;
}
else
{
k = (int)(20*(phase+1));
phase_pdf[k] += 1;
}
}
printf("test progress : current test count is %d\r",test_cnt);
}
fclose(fp_i);
fclose(fp_q);
/* 輸出到屏幕 */
printf("\n\n");
printf("Sample Sent: %d\n", SAMPLE_NUM);
printf("Average Tx Energy: %f\n", tx_energy / SAMPLE_NUM);
printf("Average Rx Energy: %f\n\n", rx_energy / SAMPLE_NUM);
/*輸出到文件供畫曲線用*/
fprintf(fp_multipath,"包絡頻率統計\n");
for (i=0; i<40; i++)
{
fprintf(fp_multipath,"%f\n",10*envelope_pdf[i]/SAMPLE_NUM);
}
fprintf(fp_multipath,"\n相位頻率統計\n");
for (i=0; i<40; i++)
{
fprintf(fp_multipath,"%f\n",20*phase_pdf[i]/SAMPLE_NUM/PI);
}
fclose(fp_multipath);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -