📄 all.m
字号:
%OFDM仿真程序
%参数设置
%SETTINGS Contains the settings to use for the COFDM transmission
% This is a common file used for the transmission and reception
% It specifies all the link properties, channel properties,
% data to send and filenames for input an output files
%
% NOTE: Not all setting combinations work as only a few have been
% fully debugged. If the receiver is giving very high error rates,
% then it is likely that there is a synchronisation error. If a
% different ifft_size, NumCarr and guardtime is used this may fix the
% error. For example: ifftsize = 2048, NumCarr = 800 guardtime = 512
% didn't sync propoerly for the Corrs.wav data set, but changing it
% to ifftsize = 1024, NumCarr = 400 and guardtime = 256, works.
% Also NumCarr must be < ifftsize/2 as the generated waveform
% is real and not complex.
%
% Due to processing and memory limitations, the maximum size input
% data is approx. 20-100kB depending on the modulation scheme.
%
% This code is not likely to work using the student version of Matlab 4.2
% due to the array size limitation of the student version.
%
% There may be some capability problems when running this code in Matlab 5.
% I think the main problem is with the scripts used to read and write the
% wave files. These could be changed to the in-built Matlab 5 wave scripts.
%
% Written by Eric Lawrey 12/8/97
%================
% COMMON SETTINGS
%================
wordsize = 8; %调制前每个符号的信息bit数目,Number of bits per carrier per symbol to send
%Allowable values are 1,2,4,8. 1 = BPSK, 2=QPSK, 4=16PSK, 8=256PSK
OutWordSize = 8; %Base wordsize of input data to the COFDM transmission,
%typically 8 bit. The input and output data is converted from
%the OutWordSize to wordsize during the transmission
ifftsize = 1024; %IFFT点数,size fourier transform to generate signal (it is equal to the
%number of samples in the symbol.) Note : must be > 2* NumCarr
NumCarr = 400; %子载波数目,Number of transmission carriers
CarrSpacing = 1; %Spacing between carriers. (1 = use all fft bins,
%2 = every second bin)
guardtime = 256; %Total guard time in samples.
%(This is typically 25% of the FFT size)
guardtype = 3; %1 = Zeroed signal, 2 = cyclic extension, 3 = half zero, half cyclic
%Note: Only type 3 has been tested fully
windowtype = 0; %0 = No window, 1 = Hanning window of symbol.
%???????用在什么地方?The window is applied to the base band time waveform.
FrameGuard = ifftsize+guardtime; %帧信号长度,Guard Time between success frames (1 symbol)
PictureComp = 1; %Fraction of picture amplitude to compress
%by to over come, wrap around from black to white
%due to a phase error (set to 1 for no compression)
DataAvg = 1; %Data Averaging, dupticate transmission of
%data words to reduce the phase error. (Normally set to 1)
SymbPerFrame = 30; %Number of data symbols per frame. The timing is resynchronized
%at the start of each frame. Set to 0 if only
%want all the data to be transmitted with one frame.
%=====================
% TRANSMITTER SETTINGS
%=====================
NoFrames = 3; %Number of duplicate data frames to generate Only valid
%if SymbPerFrame = 0
%==================
% RECEIVER SETTINGS
%==================
quickrate = round(FrameGuard/128)+1;
%Amount of subsampling to find the approx starting position of the
%start frame. The larger quickrate is the faster the search is but
%the lower the probability of finding the correct starting location.
%quickrate needs to be lower as the SNR worsens. Typical values
%are from 10 - 200
%=================
% CHANNEL SETTINGS
%=================
Comp = 0; %Peak Power Compression (in dB relative to peak signal power)
%set to zero for no signal compression or clipping
SNR = 300; %Signal to noise ratio of received signal in dB, setting > 300
%adds no noise.
Delay = 1; %Delay of single reflection multipath signal in samples
%Set to 1 for no multipath.
MultiMag = 1; %Magnitude of the reflection with respect to the direct signal
%e.g. Setting to 0.5 makes the reflection half the amplitude of the
%direct signal
%====================
% INFORMATION FORMATS
%====================
DataType =4; %Type of data to send in the transmission
% 1 = random data (Amount set by NoRandData)
% 2 = grey scale bitmap image (only the picture data is sent
% not the file header or colour map) This is useful for high
% error rate conditions
% 3 = general binary data file. This allows for any data file to be
% sent, e.g. a recorded wavefile, a gif image, a jpg image, an excel
% document etc. The file is sent simply as a binary file with no
% knowledge of the file structure. Thus an error in the file header
% could corrupt the entire file. In a practical system the forward
% error correction would be required to reduce the error to
% an acceptable level to send general data.
% To make the data link easier to implement the number of data words
% sent and the number of frames used is not transmitted, but sent via
% a file.
% 4 = Wave Sound file. This reads in an 8bit windows 3.1 wav file
NoRandData =12000; %Number of random data words to transmit if the DataType = 1.
RandSeed = 1234; %Random Seed used for generating the random data sent. Both the
%transmitter and receiver needs to know the seed. Only valid if
%DataType = 1.
Fs = 44100; %Sample rate of COFDM wav file generated
res = 16; %No. bits/sample of saved wavefile
txwavfile = 'imagetx.wav'; %Filename of the wavefile generated
rxwavfile = 'imagetx.wav'; %Filename of the wavefile to decode
infile = 'corrs11.wav'; %input filename of the bmp file or general file to transmit
outfile = 'out.wav'; %Filename to store the received image
errorfile = 'errorpic.bmp'; %Filename of the picture of the
%errors induced in the received image.
%Only valid for DataType = 2;
fileknown = 1; %Indicates whether the OFDM signal file being read is known
%i.e. whether transmitted data file exists so that the
%exact data size can be found.
%if fileknown = 0, the OFDM signal is read straight from
%the file given with no regard for transmitted data file.
%Error analysis can not be done on files read in this way
%as there is nothing to check the received data against.
%Also padding may be added to the received data.
%fileknown = 1, is the normal operation where the original
%received data is checked against the transmitted data.
%==================================================
%Calc Carriers used for a single wide COFDM channel
%==================================================
%This section calculates the carriers used to transmit the signal.
%carriers is a vector of the carriers used in the transmission, e.g. [3,5,7,8,9]
%Modifying carrier, allows the spectrum to be tailored to the requirements of the channel
MidFreq = ifftsize/4; %find the middle of the spectrum
StartCarr = MidFreq - round(((NumCarr-1)*CarrSpacing/2));
FinCarr = MidFreq + floor(((NumCarr-1)*CarrSpacing/2));
carriers = [StartCarr:CarrSpacing:FinCarr ]+1;
%carriers = [2:NumCarr];
NumCarr = length(carriers);
%发送部分
Function BaseSignal = transmit(Datatx,ifftsize,carriers,wordsize,guardtype,guardtime,windowtype,DataAvg)
%TRANSMIT Generates a COFDM waveform from an input data.
% function BaseSignal = transmit(DataIn,ifftsize,carriers,...
% wordsize,guardtype,guardtime,windowtype,DataAvg)
%
% This function generates a COFDM time waveform based on the input parameters
% and the data given. The data is transmformed into a single frame COFDM signal.
% This for the simulation of a COFDM phone system.
% INPUTS:
% ========
% Datatx : Data to transmit in the wordsize given. eg. for wordsize = 2
% Data is from 0-3, for wordsize = 8, data is from 0-255
% The data should be a single row vector. See convbase.m to
% convert from 8bit data to the required wordsize.
% e.g. datatx = convbase(datain, 8, wordsize);
% ifftsize : Size of ifft to use for generating the waveform
% carriers : Which carriers to use for the transmission
% wordsize : Number of bits to transmit on each carrier eg. 2 => QPSK
% 1 => BPSK, 4 => 16PSK, 8 => 256PSK.
% Must be one of: 1,2,4 or 8
% guardtype : What type of guard period to use
% Options:
% 0 = No Guard period
% 1 = zero level guard period
% 2 = cyclic extension of end of symbols
% 3 = same as 2 but with the first half of the guard period = zero
% guardtime : Number of sample to use for the total guard time
% windowtype : Type of window to apply to the time waveform of the symbol
% Options:
% 0 = No windowing
% 1 = Hamming window
% DataAvg : Data Averaging. Number of repeats to send of each data word, so
% that the repeats can be combined at the receiver to reduce the
% phase error, and thus to BER. DataAvg = 1 (No duplication)
%
% OUTPUTS:
% ========
% BaseSignal : This is the output time signal for the COFDM waveform. The format
% of the output is a row vector.
% Datatx : Data transmitted. This is the input data (DataIn) converted to
% the number base used for transmission, based on wordsize.
%
% Copyright (c) Eric Lawrey 1997
%
% See: RECEIVE, RDFILE.
%===============================
% External Functions Used :
% None
%===============================
% Modifications:
% 8/6/97 Redo of the previous simulation script. (based on distort.m)
% 15/6/97 Continuing work on this function, currently not finished
% 16/6/97 Continued work on function, it is now finished and works, but not all
% the features have been tested.
% I have tested the windowing and the zeroed guard period. The code has also
% been optimized a bit and runs ~10 times faster then it used to, plus
% it can handle much bigger files (tested upto 87kbytes, wordsize=4), in 35sec
% It appears to work as a function.
% The function needs to be changed so that it does not read the file directly
% but instead get the data as input.
% 17/6/97 Modified the function so that it does read the input file from within
% the transmit function. This is so that the file can be read else where
% then split up into smaller frames, then processed by transmit.
% Fixed up some logical errors in the program, and removed the output phase
% variable as it can be easily calculated from the data being transmitted.
% 12/8/97 Changed the input requirements for Datatx. It is now DataIn which is a
% serial vector of byte data. The base conversion, reshaping of the data
% into symbols, and padding of the input data is done by transmit.m. I also
% data averaging that send duplicates of the data words. The duplicates are
% sent as the very next data word. (No the best spreading, but easy to do).
%====================
% Initialization
%====================
%rand('seed',3567);
NumCarr = length(carriers); %find the number of carriers
%=====================
% Apply Data Averaging
%=====================
%Duplicate data to be transmitted, duplicate each dataword having the next carrier
%also transmitting the same data word. This will not give the best diversity
%performance but is the easiest to implement.
DataOut = zerohold(Datatx,DataAvg);
%=============
% Pad the Data
%=============
%Format the input serial data stream into symbols
%Reshape the data to fit the number of carriers
numsymb = ceil(length(DataOut)/NumCarr);
%If the data length is not a multiple of the number of carrier the pad the data
%with zeros
if length(DataOut)/NumCarr ~= numsymb,
DataPad = zeros(1,numsymb*NumCarr);
DataPad(1:length(DataOut)) = DataOut;
DataOut = DataPad;
end
clear DataPad;
%==============================
% Reshape the data into symbols
%==============================
DataOut = reshape(DataOut,NumCarr,numsymb)'; %Reshape the data into symbols
numsymb = size(DataOut,1)+1; %find the total number of symbols
%including the phase reference symbol
%========================================
% Convert to DQPSK & add phase reference
%========================================
PhaseRef = round(rand(1,NumCarr)*(2^wordsize)+0.5); %generate random phase ref.
DPSKdata = zeros(size(DataOut,1)+1,size(DataOut,2));
DPSKdata(1,:) = PhaseRef;
for k = 1:numsymb-1
DPSKdata(k+1,:)=rem((DataOut(k,:) + DPSKdata(k,:)-1),(2^wordsize))+1;
end
%clear DataOut;
%=====================================
%Find the required spectrums
%=====================================
[X,Y] = pol2cart(DPSKdata*(2*pi/(2^wordsize)),ones(size(DPSKdata)));
CarrCmplx = X+i*Y;
NegCarriers = ifftsize-carriers+2; %find the bins for the negative frequency carriers
TxSpectrums = zeros(numsymb,ifftsize);
for k = 1:numsymb
%Place the carriers used into the full spectrum
TxSpectrums(k,carriers) = CarrCmplx(k,:);
TxSpectrums(k,NegCarriers) = conj(CarrCmplx(k,:));
end
clear NegCarriers;
%==================================
%Find the time waveform using IFFT
%==================================
BaseSignal = real(ifft(TxSpectrums'));
clear TxSpectrums; %Save Memory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -