⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicmoddemod.m

📁 这是一个关于完成QAM调制的Matlab示例程序
💻 M
字号:
function [ber,ser] = basicModDemod(gammaS,RAYLEIGH,SLOWFADE,numSymb,phaseError,dopplerError,PULSE,timingError)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% gammaS = Es/N0, in dB
% RAYLEIGH = boolean value for Rayleigh fading. If = nonzero value
%            (==1), then Rayleigh fading will be applied.  Else, just awgn.
% SLOWFADE = boolean value for slow fading.  If ==1, then identical fading is
%            applied to all symbols.  If ==0, then each symbol is faded independently.
% numSymb = number of symbols to use in the simulation (the length of the 
%           xmitted sequence)
% phaseError = channel estimation phase error.
% dopplerError = channel estimation doppler error, in cycles/symbol
% pulseFilt = pulse shaping filter coefficients
% PULSE = boolean value.  ==1 if pulse shaping active, ==0 else
% timingError = desired timing mismatch in receiver matched filter sampling; in number of samples.
%
% Function overview:
% Generate source symbols, 
% QAM modulate
% Add noise, perhaps fading
% QAM demod with ML detect
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Initialize some basic variables
SHOW_GRAPHICS = 0;
Frequencies;
%RAYLEIGH = 0;
%fd = 1; % data frequency
%fc = 4*fd; % carrier frequency
%fs = 8*fc; % 4x oversampling
%numSymb = 10000; % Be sure to scale up numSymb for hi gammaS so that 
                 % ber, ser is statistically valid.
m = 4;
%gammaS = 20; % in dB, Es/N0 by definition, equals snr if bandwidth = 1/Ts
snr = gammaS;
%snrPerBit = 15; % in dB?
%snrPerSymb = snrPerBit/log2(m);


% Generate my source
sourceSymb = randsrc(numSymb,1,[0:m-1]);

% Plot a little bit of it
if SHOW_GRAPHICS
    figure;
    stem(1:10,sourceSymb(1:10));
    figure;
    stem(91:100,sourceSymb(91:100));
end % SHOW_GRAPHICS

% Apply gray encoding
GrayEncode = bitxor([0:m-1],floor([0:m-1]/2));
graySourceSymb = GrayEncode(sourceSymb+1);
if SHOW_GRAPHICS
    figure;
    t = 1:(10);  % want to look at the first 10 symbols
    plot(t,graySourceSymb(t));
end
% Modulate my source, no pulse shaping for now.
% The demo shows gray encoding.  However, I do not see the need
% since my source is truely random.  That is, with the MSB being one bit,
% and the lsb considered the other, I am automatically gray encoded.
%
% It's unclear to me if the amplitude and phase in the following call are
% appropriately specified.  Try this as-is and examine results.
if (~PULSE)
    msgTx = dmodce(graySourceSymb,fd,fs,'qask/cir',m,sqrt(2),pi/4)';

	if SHOW_GRAPHICS
        figure;
        t = 1:(10*fs);  % want to look at the first 10 symbols
        plot(t/fs,real(msgTx(t)),'b+',t/fs,imag(msgTx(t)),'c-');
	end

	% Here is where I would add noise and fading.
	% Their documentation is not clear on how ddemodce filters incoming data.  Slide
	% 4 of the demo implies that it just sums all samples for that symbol, that is,
	% a square matched filter.  This is implied by the SNR atten of -10log10(fs/fd) term.
	% SER from simulation confirms q-function results.
	%rxMsg = awgn(msgTx,snr-10*log10(fs/fd),10*log10(2));
	idealRxMsg = Channel(msgTx,snr-10*log10(fs/fd),fs/fd,RAYLEIGH,SLOWFADE);
	rxMsg = EstimationError(idealRxMsg,phaseError,dopplerError/(fs/fd));
	
	% QAM demod, ML detect
	%grayRxSymb = ddemodce(noisyMsgTx,fd,fs,'qask/cir',m,sqrt(2),pi/4);
	
	grayRxSymb = ddemodce(rxMsg,fd,fs,'qask/cir',m,sqrt(2),pi/4);
	if SHOW_GRAPHICS
        figure;
        t = 1:(10);  % want to look at the first 10 symbols
        plot(t,grayRxSymb(t));
	end
else
    % I think having this else case is a hack.  For good programming style, the calls to the above case should handle
    % things within themselves.  That is, I don't like this extra code laying around to handle a one-off case.  However,
    % the project's due in a couple of days, so a hack will do.
    msgTx = dmodce(graySourceSymb,fd,fd,'qask/cir',m,sqrt(2),pi/4); % Not sure if sampling freq = fd is ok.  Verify
 	if SHOW_GRAPHICS
        figure;
        t = 1:(10);  % want to look at the first 10 symbols
        plot(t,real(msgTx(t)),'b+',t,imag(msgTx(t)),'c-');
	end
    % INSERT RCOSFILTERING HERE.  MAY DO A CALL TO FIRST GEN FILTER COEFF'S, THEN SCALE APPROPRIATELY, THEN FILTER.
    % WANT TO SCALE SUCH THAT SUM OF COEFF'S = 1
    [pulseFilt,delay] = CreatePulseFilter(fs,fd);
    msgTx = rcosflt(msgTx,fd,fs,'filter_type/filter',pulseFilt);
    
	if SHOW_GRAPHICS
        figure;
        t = 1:(20*fs);  % want to look at the first 10 symbols
        plot(t/fs,real(msgTx(t)),'b+',t/fs,imag(msgTx(t)),'c-');
	end

	% Here is where I would add noise and fading.
	idealRxMsg = Channel2(msgTx,snr,fs/fd,RAYLEIGH,SLOWFADE);
	
    % Matched filter
    rxMsg = rcosflt(idealRxMsg,fs,fs,'filter_type/filter',pulseFilt);
  	if SHOW_GRAPHICS
        figure;
        t = 1:(20*fs);  % want to look at the first 10 symbols
        plot(t/fs,real(rxMsg(t)),'b+',t/fs,imag(rxMsg(t)),'c-');
	end
	rxMsg = EstimationErrorWithMatchedFilt(rxMsg,phaseError,dopplerError/(fs/fd),timingError,delay,fs/fd,length(graySourceSymb));
    %	rxMsg = EstimationError(idealRxMsg,phaseError,dopplerError/(fs/fd),pulseFilt,1);
    %rxMsg = msgTx
	
	% QAM demod, ML detect
	%grayRxSymb = ddemodce(noisyMsgTx,fd,fs,'qask/cir',m,sqrt(2),pi/4);
	
  	if SHOW_GRAPHICS
        figure;
        t = 1:(10);  % want to look at the first 10 symbols
        plot(t,real(rxMsg(t)),'b+',t,imag(rxMsg(t)),'c-');
    end

    % Since I've grabbed only the detection statistic, I've already taken care of subsampling.  So, demod sampling
    % freq = fd.
	grayRxSymb = ddemodce(rxMsg,fd,fd,'qask/cir',m,sqrt(2),pi/4);
	%grayRxSymb = ddemodce(rxMsg,fd,fs,'qask/cir',m,sqrt(2),pi/4,pulseFilt,1);
	if SHOW_GRAPHICS
        figure;
        t = 1:(10);  % want to look at the first 10 symbols
        plot(t,grayRxSymb(t));
	end

end

% Gray decode
[dummy GrayDecode] = sort(GrayEncode);
GrayDecode = GrayDecode - 1;
rxSymb = GrayDecode(grayRxSymb+1)';

% Plot a little bit of the received signal
if SHOW_GRAPHICS
    figure;
    stem(1:10,rxSymb(1:10));
    figure;
    stem(91:100,rxSymb(91:100));
end % SHOW_GRAPHICS

% Do BER calcs
[numBitErrors ber] = biterr(sourceSymb,rxSymb,log2(m));
[numSymErrors ser] = symerr(sourceSymb,rxSymb);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -