Re: [問題] 關於浮點數的運算
原文吃光~~
我的程式碼如下:
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
//----------------------------------
#define N 100000
#define fs 10
#define ts 0.1
#define pi 3.141592653589793
#define length N*fs
#define SNR_dB 12
//----------------------------------bpsk random sequence and WGN
double Eb=1.0;
int bpsk(double mean,double sigma)
{
int i,m,g;
static double ak[N]={0.0};
static double sn[length]={0.0};
static double x[length]={0.0};
static double y[length]={0.0};
static double R[length]={0.0};
static double theta[length]={0.0};
static double randn[length]={0.0};
static double r[length]={0.0};
static double de_sn[length]={0.0};
int error = 0;
unsigned seed;
srand(time(NULL));
//----------------------------------continuous signal
for(m=0;m<N;m++)
{
ak[m]=rand()%2*2.0-1.0;
for(g=0;g<fs;g++)
{
sn[g+(m*fs)]=ak[m]*sqrt(2.0*Eb);
}
}
//----------------------------------AWGN
for(i=0;i<length;i++)
{
x[i]=rand()/32767.0;
y[i]=rand()/32767.0;
R[i]=sigma*sqrt(2.0*log(1.0/(1.0-x[i])));
theta[i]=2.0*pi*y[i];
randn[i]=R[i]*cos(theta[i])+mean;
r[i]=sn[i]+randn[i];
if(r[i]>0)
{
de_sn[i]=1.000000*sqrt(2*Eb);
}
else
{
de_sn[i]=-1.00000*sqrt(2*Eb);
}
if(de_sn[i]!=sn[i])
{
error=error+1;
}
else
{
error=error;
}
}
return error;
}
//--------------------------------------------------start
int main(void)
{
int k;
double mean[SNR_dB]={0.0};
double N0[SNR_dB]={0.0};
double sigma[SNR_dB]={0.0};
double SNRdB[SNR_dB]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0};
double SNR[SNR_dB]={0.0};
static double ber;
FILE *fp;
char *filename="ber.dat";
fp=fopen(filename,"wt");
for(k=0;k<SNR_dB;k++)
{
SNR[k]=pow(10.0,SNRdB[k]/10.0);
N0[k]=Eb/SNR[k];
sigma[k]=sqrt(N0[k]);
ber=(double)bpsk(mean[k],sigma[k])/(double)(N*fs);
fprintf(fp,"%f\t%d\n",ber,bpsk(mean[k],sigma[k]));
}
fclose(fp);
return 0;
}
很多寫法還蠻新手的 希望別介意...
主要就是先產生不同的能量 再將能量的標準差(sigma表示)丟入bpsk副程式~
副程式裡面做的事情就是先產生亂數訊號1 and -1 乘上sqrt(2)
如果是1就產生10個sqrt(2) 負1就產生10個-sqrt(2)
再來 產生雜訊 跟訊號混在一起~
再判斷跟原訊號有無相同~ 不相同即錯誤+1 最後將錯誤丟回主程式 算錯誤率~
能量SNR_dB在1~9時都OK~ 10開始就誤差很大了...
希望有人能解惑囉~
也謝謝各位看完我這新手程式碼....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.18.104.154
推
07/08 10:14, , 1F
07/08 10:14, 1F
→
07/08 10:43, , 2F
07/08 10:43, 2F
→
07/08 12:05, , 3F
07/08 12:05, 3F
→
07/08 12:06, , 4F
07/08 12:06, 4F
推
07/08 13:06, , 5F
07/08 13:06, 5F
→
07/08 13:39, , 6F
07/08 13:39, 6F
→
07/08 13:39, , 7F
07/08 13:39, 7F
→
07/08 13:51, , 8F
07/08 13:51, 8F
→
07/08 13:52, , 9F
07/08 13:52, 9F
→
07/08 14:35, , 10F
07/08 14:35, 10F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章