通信建模作业程序集锦(共5页).docx
精选优质文档-倾情为你奉上1.正态分布:#include <math.h> #include <stdlib.h> #include <time.h> #include <fstream> #include <iostream> using namespace std; #define pi 3. /Generate a randon number followed by uniform distribution between the inteval min,max template<typename T> T rand(T min, T max) return min+(max-min)*rand()/(RAND_MAX+1.0); /calculate the normal density distribution values double normal(double x, double miu,double sigma) return 1.0/sqrt(2*pi)/sigma*exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma); /Generate a distributed random output numberdouble randn(double miu,double sigma, double min ,double max) double x,y,dScope; do x=rand(min,max); y=normal(x,miu,sigma); dScope=rand(0.0,normal(miu,miu,sigma); while(dScope>y); return x; /Generate 10000 distributed random output numbers and put into a text called"123"int main(int argc,char* argv) double min,max,miu,sigma; srand(unsigned)time( NULL ); cin>>min>>max>>miu>>sigma; ofstream outfile("123.txt"); for (int i=0;i<10000;i+) outfile << randn(miu,sigma,min,max) << endl; return 0; 2.瑞利分布:#include <math.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define pi 3./ generate the random numbers between the interval 0,1double randn()double f;f = (double)(rand()%1001); /0,1000return f/1000.0; /0,1/calculate the Rayleigh density function values by using the following generated random numbers double ruili(double sigma) Return randn()/(sigma*sigma)*exp(-randn()*randn()/(2*sigma*sigma); /The main function can generate 10000 distributed random output numbers which are put into a text called”321”int main(void) double sigma; srand(unsigned)time( NULL ); printf("input sigma:n"); scanf("%lf",&sigma); for (int i=0;i<10000;i+) FILE *fp;fp=fopen("321.txt","a+");fprintf(fp,"%lf,",ruili(sigma);printf("%lfn",randn();fclose(fp); return 0;3.泊松分布:#include<stdio.h>#include<stdlib.h>#include<ctime>#include<math.h>/Generate the random numberslong Possion(double u)double P,U;long N;u=exp(-u);P=1;N=0;while(1)U=(double)rand()/RAND_MAX;P=P*U;if(P<=u) break;N+;return N;/ Generate 10000 distributed random output numbers which are put into a text called”312”void main() int k; double lamda; scanf("%lf",&lamda); FILE *fp1; fp1=fopen("p1.txt","w"); srand(unsigned)time(0); for(k=1;k<10000;k+) fprintf(fp1,"%6ld ",Possion(lamda); fclose(fp1); 4. Design a 128th order FIR band-pass digital filter with cut-off frequencies 10 MHz & 50 MHz Apply it to a signal, and display related filter and signal waveforms.fc=1000; %载波频率Êfc=1000HZqifs=280; %采样频率fs=280HZt=1:2:1000; x=sin(2*pi*t*fc); %generate signal waveforms.a=fir1(128,10 50/(qifs/2) ; %Bandpass butt.freqz (a,1) ; % plot gain & phase (the b array is 1 for FIR filter)y=filter(a,1, x); % FIR band-pass filter design. 5. Design a 4th order IIR low-pass filter with fc = 100 Hz.Apply it to a signal, and display related filter and signal waveforms.fc =100; %载波频率fc=100HZqifs=1000; %采样频率fs=1KHZt=(1:1000)/500; x=sin(2*pi*t*fc) %generate signal waveforms.a,b=butter(4, fc/(qifs/2) ) ; % lowpass Butt.freqz (a,b) ; % plot gain & phase responses of digital filtersy=filter(a,b,x); % to implement6. Based on Program 2.3/2.4 of Ref.9 (by Yong Soo Cho, et al), investigate the impact of of the RMS delay spread and T s tothe channel frequency selectivity.% plot_IEEE80211_model.mclear, clfscale=1e-9; % nanoTs=100*scale; % Sampling timet_rms=250*scale; % RMS delay spreadnum_ch=10000; % Number of channelsN=128; % FFT sizePDP=IEEE802_11_model(t_rms,Ts); for k=1:length(PDP) h(:,k) = Ray_model(num_ch).'*sqrt(PDP(k); avg_pow_h(k)= mean(h(:,k).*conj(h(:,k);endH=fft(h(1,:),N);subplot(221)stem(0:length(PDP)-1,PDP,'ko'), hold on,stem(0:length(PDP)-1,avg_pow_h,'k.');xlabel('channel tap index, p'), ylabel('Average Channel Powerlinear');title('IEEE 802.11 Model, sigma_tau=25ns, T_S=25ns');legend('Ideal','Simulation'); axis(-1 7 0 1);subplot(222)plot(-N/2+1:N/2/N/Ts/106,10*log10(H.*conj(H),'k-');xlabel('FrequencyMHz'), ylabel('Channel powerdB')title('Frequency response, sigma_tau=25ns, T_S=25ns');7. Simulate a BPSK communication system, comparing simulated with theoretical BER results under AWGN channel.clc;clear all;close all;num=;for SNR=0:10 data_bpsk=randsrc(1,num,1,-1); snr=1/(10(SNR/10); noise=sqrt(snr/2)*(randn(1,num); receive=data_bpsk+noise; pe(SNR+1)=0; for(i=1:num) if (receive(i)>=0) r_data(i)=1; else r_data(i)=-1; end end pe(SNR+1)=(sum(abs(r_data-data_bpsk)/2)/num; peb(SNR+1)=0.5*erfc(sqrt(10(SNR/10);endr=0:10;semilogy(r,peb,'b-v',r,pe,'m-x');%对y取底为10对数grid on;legend('理论误码率曲线','仿真误码率曲线');8. Simulate the error performance of a QPSK system using Quasi-analytical method, under AWGN channel. At least 200 errors should be generated for each EbNo% QPSK System Simulation, Pe evaluationclear;clc;SNRindB1=0:0.05:10; %仿真信噪比范围SNRindB2=0:0.1:10; %理论信噪比范围for i=1:length(SNRindB1), pb=cm_sm32(SNRindB1(i); % simulated bit error rates smld_bit_err_prb(i)=pb; disp(pb); echo off;end;%echo on;For i=1:length(SNRindB2), SNR=exp(SNRindB2(i)*log(10)/10); % signal to noise ratio theo_err_prb(i)=0.5*erfc(sqrt(SNR); % theoretical bit error rate echo off;end;% Plotting commands follow %作图grid onsemilogy(SNRindB1,smld_bit_err_prb,'*'); % 用对数坐标作出仿真信噪比-误比特率的点hold onsemilogy(SNRindB2,theo_err_prb); %用对数坐标作出理论信噪比-误比特率曲线hold onxlabel('信噪比(dB)');ylabel('误码率');legend('bit error probability','theory error probability');title('BER performance of QPSK transmission scheme under AWGN channel');function pb,ps=cm_sm32(snr_in_dB)% pb,ps=cm_sm32(snr_in_dB)% CM_SM3 finds the probability of bit error and symbol error for % the given value of snr_in_dB, signal to noise ratio in dB.N=10000;E=1; % energy per symbolsnr=10(snr_in_dB/10); % signal to noise ratiosgma=sqrt(E/snr)/2; % noise variances00=1 0; s01=0 1; s11=-1 0; s10=0 -1; % signal mapping% generation of the data source, QPSK modulationfor i=1:N, temp=rand; % a uniform random variable between 0 and 1 if (temp<0.25), % with probability 1/4, source output is "00" dsource1(i)=0; dsource2(i)=0; elseif (temp<0.5), % with probability 1/4, source output is "01" dsource1(i)=0; dsource2(i)=1; elseif (temp<0.75), % with probability 1/4, source output is "10" dsource1(i)=1; dsource2(i)=0; else % with probability 1/4, source output is "11" dsource1(i)=1; dsource2(i)=1; end;end;% detection and the probability of error calculationnumofsymbolerror=0;numofbiterror=0;% go through AWGN channelfor i=1:N, % the received signal at the detection, for the ith symbol,is: n=sgma*randn(1,2); % 2 normal distributed r.v with 0, variance sgma if (dsource1(i)=0) & (dsource2(i)=0), r=s00+n; elseif (dsource1(i)=0) & (dsource2(i)=1), r=s01+n; elseif (dsource1(i)=1) & (dsource2(i)=0), r=s10+n; else r=s11+n; end; % The correlation metrics are computed below c00=dot(r,s00); c01=dot(r,s01); c10=dot(r,s10); c11=dot(r,s11); % The decision on the ith symbol is made next c_max=max(c00,c01,c10,c11);% The decision on the ith symbol, MLD if (c00=c_max), decis1=0; decis2=0; elseif (c01=c_max), decis1=0; decis2=1; elseif (c10=c_max), decis1=1; decis2=0; else decis1=1; decis2=1; end;% Later, increment the error counter, if the decision is not correct, % Increment the error counter, if the decision is not correct symbolerror=0; if (decis1=dsource1(i), numofbiterror=numofbiterror+1; symbolerror=1; end; if (decis2=dsource2(i), numofbiterror=numofbiterror+1; symbolerror=1; end; if (symbolerror=1), numofsymbolerror=numofsymbolerror+1; end;end;ps=numofsymbolerror/N; % since there are totally N symbolspb=numofbiterror/(2*N); % since 2N bits are transmitted9. Simulate the error performance of an 8-QAM system using the built-in Matlab channel function " rayleighchan ".% 8QAM System Simulation, Pe evaluation M=8; % QAM 调制阶数 k = log2(M); hMod=comm.RectangularQAMModulator(M); % 产生 8 QAM 信号调制器 display(hMod); txd = randint(1e4,1,M); %产生1,M范围的均匀分布的随机序列,用于在接收端发送基带信号 qamSig = step(hMod, txd); % 8 QAM调制信号 chan = rayleighchan(1/50000,100); % 产生瑞利分布的信道模型 SNRindB1=0:0.2:12; %仿真信噪比范围 SNRindB2=0:0.1:12; %理论信噪比范围for i=1:length(SNRindB1), hDemod=comm.RectangularQAMDemodulator(8); % 产生 8 QAM 信号解调器 fadedSig = filter(chan,qamSig); %将调制信号加入瑞利信道后再进行滤波得到瑞利衰落信号 rxd = step(hDemod, fadedSig); %接受解调后的信号 smld_bit_err_prb(i) = biterr(txd,rxd); %计算仿真误比特率end;%计算理论误比特率for i=1:length(SNRindB2), SNR=exp(SNRindB2(i)*log(10)/10); % signal to noise ratio theo_err_prb(i)=2*erfc(sqrt(9*SNR/7)/sqrt(2); % theoretical bit error rate echo off;end;%echo on;% Plotting commands followgrid onsemilogy(SNRindB1,smld_bit_err_prb,'*'); % 用对数坐标作出仿真信噪比-误比特率的点hold onsemilogy(SNRindB2,theo_err_prb); %用对数坐标作出理论信噪比-误比特率曲线hold onxlabel('信噪比(dB)');ylabel('误码率');legend('bit error probability','theory error probability');title('BER performance of 8QAM system scheme under rayleigh channel');专心-专注-专业