请教Matlab程序错误问题,本人菜鸟

% MATLAB script for Illustrative Problem 9.4.
echo on
Lc=20; % number of chips per bit
A1=3; % amplitude of the first sinusoidal interference
A2=7; % amplitude of the second sinusoidal interference
A3=12; % amplitude of the third sinusoidal interference
A4=0; % fourth case: no interference
w0=1; % frequency of the sinusoidal interference in radians
SNRindB=0:2:30;
for i=1:length(SNRindB),
% measured error rates
smld_err_prb1(i)=ss_Pe94(SNRindB(i),Lc,A1,w0);
smld_err_prb2(i)=ss_Pe94(SNRindB(i),Lc,A2,w0);
smld_err_prb3(i)=ss_Pe94(SNRindB(i),Lc,A3,w0);
echo off ;
end;
echo on ;
SNRindB4=0:1:8;
for i=1:length(SNRindB4),
% measured error rate when there is no interference
smld_err_prb4(i)=ss_Pe94(SNRindB4(i),Lc,A4,w0);
echo off ;
end;
echo on ;
% Plotting commands follow.
function [p]=ss_Pe94(snr_in_dB, Lc, A, w0)
% [p]=ss_Pe94(snr_in_dB, Lc, A, w0)
% SS_PE94 finds the measured error rate. The function
% that returns the measured probability of error for the given value of
% the snr_in_dB, Lc, A and w0.
snr=10^(snr_in_dB/10);
sgma=1; % Noise standard deviation is fixed.
Eb=2*sgma^2*snr; % signal level required to achieve the given
% signal-to-noise ratio
E_chip=Eb/Lc; % energy per chip
N=10000; % number of bits transmitted
% The generation of the data, noise, interference, decoding process and error
% counting is performed all together in order to decrease the run time of the
% program. This is accomplished by avoiding very large sized vectors.
num_of_err=0;
for i=1:N,
% Generate the next data bit.
temp=rand;
if (temp<0.5),
data=-1;
else
data=1;
end;
% Repeat it Lc times, i.e. divide it into chips.
for j=1:Lc,
repeated_data(j)=data;
end;
% pn sequence for the duration of the bit is generated next
for j=1:Lc,
temp=rand;
if (temp<0.5),
pn_seq(j)=-1;
else
pn_seq(j)=1;
end;
end;
% the transmitted signal is
trans_sig=sqrt(E_chip)*repeated_data.*pn_seq;
% AWGN with variance sgma^2
noise=sgma*randn(1,Lc);
% interference
n=(i-1)*Lc+1:i*Lc;
interference=A*sin(w0*n);
% received signal
rec_sig=trans_sig+noise+interference;
% Determine the decision variable from the received signal.
temp=rec_sig.*pn_seq;
decision_variable=sum(temp);
% making decision
if (decision_variable<0),
decision=-1;
else
decision=1;
end;
% If it is an error, increment the error counter.
if (decision~=data),
num_of_err=num_of_err+1;
end;
end;
% then the measured error probability is
p=num_of_err/N;
后面出现这个问题
A function declaration cannot appear within a script M-file.
请问如何解决,最好详细一点,真是一窍不通

这个类似于C语言中的主程序和子程序的关系。你这段程序是为了分析某系统的误码率。因此应将程序中第26行起:function [p]=ss_Pe94(snr_in_dB, Lc, A, w0)
% [p]=ss_Pe94(snr_in_dB, Lc, A, w0)
% SS_PE94 finds the measured error rate. The function
% that returns the measured probability of error for the given value of
% the snr_in_dB, Lc, A and w0.
snr=10^(snr_in_dB/10);
sgma=1; % Noise standard deviation is fixed.
Eb=2*sgma^2*snr; % signal level required to achieve the given
% signal-to-noise ratio
E_chip=Eb/Lc; % energy per chip
N=10000; % number of bits transmitted
% The generation of the data, noise, interference, decoding process and error
% counting is performed all together in order to decrease the run time of the
% program. This is accomplished by avoiding very large sized vectors.
num_of_err=0;
for i=1:N,
% Generate the next data bit.
temp=rand;
if (temp<0.5),
data=-1;
else
data=1;
end;
% Repeat it Lc times, i.e. divide it into chips.
for j=1:Lc,
repeated_data(j)=data;
end;
% pn sequence for the duration of the bit is generated next
for j=1:Lc,
temp=rand;
if (temp<0.5),
pn_seq(j)=-1;
else
pn_seq(j)=1;
end;
end;
% the transmitted signal is
trans_sig=sqrt(E_chip)*repeated_data.*pn_seq;
% AWGN with variance sgma^2
noise=sgma*randn(1,Lc);
% interference
n=(i-1)*Lc+1:i*Lc;
interference=A*sin(w0*n);
% received signal
rec_sig=trans_sig+noise+interference;
% Determine the decision variable from the received signal.
temp=rec_sig.*pn_seq;
decision_variable=sum(temp);
% making decision
if (decision_variable<0),
decision=-1;
else
decision=1;
end;
% If it is an error, increment the error counter.
if (decision~=data),
num_of_err=num_of_err+1;
end;
end;
% then the measured error probability is
p=num_of_err/N;
这一段单独保存为一个文件,而且这个文件是一个函数文件,文件名应于函数名相同,即:ss_Pe94.m,剩下那一段:% MATLAB script for Illustrative Problem 9.4.
echo on
Lc=20; % number of chips per bit
A1=3; % amplitude of the first sinusoidal interference
A2=7; % amplitude of the second sinusoidal interference
A3=12; % amplitude of the third sinusoidal interference
A4=0; % fourth case: no interference
w0=1; % frequency of the sinusoidal interference in radians
SNRindB=0:2:30;
for i=1:length(SNRindB),
% measured error rates
smld_err_prb1(i)=ss_Pe94(SNRindB(i),Lc,A1,w0);
smld_err_prb2(i)=ss_Pe94(SNRindB(i),Lc,A2,w0);
smld_err_prb3(i)=ss_Pe94(SNRindB(i),Lc,A3,w0);
echo off ;
end;
echo on ;
SNRindB4=0:1:8;
for i=1:length(SNRindB4),
% measured error rate when there is no interference
smld_err_prb4(i)=ss_Pe94(SNRindB4(i),Lc,A4,w0);
echo off ;
end;
echo on ;
% Plotting commands follow.
也就是你的主程序,如命名为test.m,保存好后,运行该文件,就可以了。在这个文件运行过程中,会调用文件ss_Pe94.m。
另:这个程序并不完整,作者写这个程序的最后一句注释% Plotting commands follow.意思是让读者自己编写画图命令来绘制出误码率图形。如:semilogy(SNRindB4,smld_err_prb4,'*');
hold on
semilogy(SNRindB,smld_err_prb1);

参考资料:现代通信系统(matlab版)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-24
把function [p]=ss_Pe94(snr_in_dB, Lc, A, w0)函数部分单独放在一个M文件中
相似回答