How do I get my simulation line to follow the theory curve?

1 次查看(过去 30 天)
For some reason my simulation line is not following the theory line and I'm not sure how to go about fixing it. Any Tips and help is very appreciated.
*****************************************************************
%fsk modulation and demodulation
close all;
%number of bits Noit=1e6;
%Generate 1 and 0s
x=round(rand(1,Noit));
%find indices of zeros
zero_ind=find(x==0);
%generate a waveform
T=0.002;
% symbol duration in sec
f0=5000; %in Hz
f1=4500; %in Hz
t=0:1/(100*f0):T; %time window, the duration between two samples is 1/(100*f0)
%Plot symbol waveform
phi= (pi/3);
tn=[];
sn=[];
for mn=1:5
twin=(mn-1)*T+0:1/(100*f0):mn*T; %time widow for each bit
twin2=(mn-1)*T+0:1/(100*f1):mn*T;
tn=[tn,twin]; %total time for three bits
tn2=[tn,twin2];
sn=[sn, cos(2*pi*f0*twin)]; %total waveform for three bits
sn2=[sn, cos(2*pi*f1*twin2)];
end
figure;
plot(tn2,sn2, 'LineWidth',2); %Plot FSK modulated signal
xlabel('time');
ylabel('FSK modulated signal');
grid on;
s1=(1/(sqrt(sum(cos(2*pi*f1*twin).^2))))*(cos(2*pi*f1*t+phi));
plot(t,s1);
s2=(1/(sqrt(sum(cos(2*pi*f0*twin2).^2))))*(cos(2*pi*f0*t+phi));
figure;
plot(t,s1,'g');
plot(t,s2,'b');
data=rand(1,6);
data=round(data);
Ncos=length(s1);
stream=0;
for i=1:length(data)
if data(i)==0
stream=[stream zeros(1,Ncos)];
else
stream=[stream ones(1,Ncos)];
end
end
stream(1)=[];
figure;
subplot(2,1,1)
stem(stream)
title('DATA BITS')
fsk=0;
for i=1:length(data)
if data(i)==1
fsk=[fsk s2];
else
fsk=[fsk s1];
end
end
fsk(1)=[];
subplot(2,1,2)
plot(1:length(stream),fsk)
title('FSK OUTPUT')
snrdB=-4:2:18; %define SNR in terms of dB
snr=10.^(snrdB/10); %change SNR from dB to linear scale
%Initialize the vector of simulated bit error values
biterrSim=zeros(1, length(snr));
%Loop over SNR
for mn=1:length(snr)
%Initialize the vector of detected bits to zeros
detect_bits=zeros(1, Noit);
%FSK modulation and demodulation for each SNR value
%Loop over FSK data
for kn=1:Noit
if (data)kn==1
%time-window for each FSK data
twin=(kn-1)*T+0:1/(100*f0):kn*T;
%Normalization is necessary since cos(2*pi*f0*twin) is not %normalized
normF=sqrt(sum(cos(2*pi*f0*twin).^2));
sM=x(kn)*(1/normF)*cos(2*pi*f0*twin);
% Generate random Gaussin Noise of zero-mean and variance 1
noise=randn(1,length(twin));
%Received signal after AWGN
%We change the power of the modulated signal while keeping
%the variance of noise to 1.
rM=sqrt(snr(mn))*sM+noise;
rM1=rM.*(cos(2*pi*f0*twin)*1/normF);
rM2=rM.*(sin(2*pi*f0*twin)*(1/normF));
z1=mean(rM1+rM2)
else
if (data)kn==0
twin2=(kn-1)*T+0:1/(100*f1):kn*T;
%Normalization is necessary since cos(2*pi*f0*twin) is not
%normalized
normF1=sqrt(sum(cos(2*pi*f1*twin2).^2));
sM1=x(kn)*(1/normF1)*cos(2*pi*f1*twin2);
% Generate random Gaussin Noise of zero-mean and variance 1
noise=randn(1,length(twin));
%Received signal after AWGN
%We change the power of the modulated signal while keeping
%the variance of noise to 1.
rMx=sqrt(snr(mn))*sM+noise;
rM3=rMx.*(cos(2*pi*f1*twin2)*1/normF1);
rM4=rMx.*(sin(2*pi*f1*twin2)*1/normF1);
z2=mean(rM3+rM4);
if(z1>=z2)
%symbol 1 was transmitted detect_bits(kn)=1;
else
%symbol (0) was transmitted
detect_bits(kn)= 0;
end
end
end
end
%Find bit error
%find(detect_bits~=x) finds the indexes where detected bits are not
% equal to actual transmitted data x
%length(find(detect_bits~=x)) find the number of positions in which
% detected bits are not equal to x
biterrSim(mn)=length(find(detect_bits~=x))/Noit;
end
%Calculate theoretical BER for FSK
% You have to change the Q fundtion of lectures notes to MATLAB's erfc( )
biterr_theo=.5*erfc(sqrt(snr/2));
figure;
semilogy(snrdB, biterrSim);
hold on;
semilogy(snrdB, biterr_theo);
legend('Simulation', 'Theory');
xlabel('SNR');
ylabel('Probability of bit error');
grid on;

回答(1 个)

vidyesh
vidyesh 2023-12-29
Hello Pierre,
It appears that there is a discrepancy between the simulated and theoretical values of the Bit Error Rate (BER) in your analysis. Notably, the simulated BER remains around 0.5 across all Signal-to-Noise Ratio (SNR) levels, which suggests a potential error in the decoding process.
Upon reviewing the code, the conditional expressions '(data)kn == 0' and '(data)kn == 1' stand out as possible sources of the issue. In MATLAB, array elements are accessed using parentheses for indexing, and there should not be any characters between the array name and the parentheses. For example, the correct syntax to access the kn element of the data array is 'data(kn)'.
For further guidance, you may refer to the following example of FSK modulation and demodulation:
Hope this answer helps

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by