QAM.asv
上传用户:m_sun_001
上传日期:2014-07-30
资源大小:1115k
文件大小:5k
- % QAM.m compares OFDM (multicarrier) to multi-level QAM (single carrier)
- % when they transmit the same # of bits in a given time period
- setup
- read % read data for QAM - does not affect OFDM
- data_in_pol = bin2pol(data_in); % Converts binary data to polar data
- % check to see if num_carriers is a power of 2
- is_pow_2 = num_carriers;
- temp_do_QAM = 0;
- if is_pow_2 ~= 2
- while temp_do_QAM == 0
- temp_do_QAM = rem(is_pow_2,2);
- is_pow_2 = is_pow_2/2;
- if is_pow_2 == 2
- temp_do_QAM = -99; % it is a power of 2 -> can do QAM
- end
- end
- else
- temp_do_QAM = -99; % 2 is a power of 2
- end
- if temp_do_QAM ~= -99
- do_QAM = 0; % don't do it if it's not possible
- disp(' '),disp('ERROR: Cannot run QAM because num_carriers is not valid.')
- disp(' Please see "setup.m" for details.')
- end
- if do_QAM == 1
- tic % Start stopwatch to calculate how long QAM simulation takes
-
- disp(' '), disp('------------------------------------------------------------')
- disp('QAM simulation'), disp('Transmitting')
-
-
- % Pad with zeros so data can be divided evenly
- data_length = length(data_in_pol);
- r = rem(data_length,num_carriers);
- if r ~= 0
- for i = 1:num_carriers-r
- data_in_pol(data_length+i) = 0; %pad input with zeros to complete last data set
- end %speed improve possible
- end
- data_length = length(data_in_pol); %update after padding
-
- num_OFDM_symbols = ceil(data_length / (2*num_carriers));
- % num QAM symbols that represent equal amount of data to one OFDM symbol
- num_QAM_symbols = num_carriers / 2;
- % num samples per QAM symbol
- num_symbol_samples = fft_size / num_QAM_symbols;
-
- % convert polar data [-1, 1] to 4 level data [-3, -1, 1, 3]
- data_in_4 = zeros(1,data_length/2);
- for i = 1:2:data_length
- data_in_4(i - (i-1)/2) = data_in_pol(i)*2 + data_in_pol(i+1);
- end
-
- data_in_4
- data_len=length(data_in_4)
- % define sample points between 0 and 2*pi
- ts = linspace(0, 2*pi*QAM_periods, num_symbol_samples+1)
-
- % Generate 16-QAM data
- % total length of 16-QAM transmission
- tx_length = num_OFDM_symbols * num_QAM_symbols * num_symbol_samples;
- QAM_tx_data = zeros(1,tx_length);
- for i = 1:2:data_length/2
- for k = 1:num_symbol_samples
- QAM_tx_data(k+((i-1)/2)*num_symbol_samples) = data_in_4(i)*cos(ts(k)) + data_in_4(i+1)*sin(ts(k));
- end
- end
-
- % Do channel simulation on QAM data
- xmit = QAM_tx_data % ch uses 'xmit' data and returns 'recv'
- ch
- QAM_rx_data = recv; % save QAM data after channel
- clear recv % remove 'recv' so it won't interfere with OFDM
- clear xmit % remove 'xmit' so it won't interfere with OFDM
-
- disp('Receiving') % Recover Binary data (Decode QAM)
- cos_temp = zeros(1,num_symbol_samples); %
- sin_temp = cos_temp; %
- xxx = zeros(1,data_length/4); % Initialize to zeros for speed
- yyy = xxx; %
- QAM_data_out_4 = zeros(1,data_length/2); %
-
- for i = 1:2:data_length/2 % "cheating"
- for k = 1:num_symbol_samples
- % multiply by carriers to produce high frequency term and original data
- cos_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * cos(ts(k));
- sin_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * sin(ts(k));
- end
- % LPF and decide - we will do very simple LPF by averaging
- xxx(1+(i-1)/2) = mean(cos_temp);
- yyy(1+(i-1)/2) = mean(sin_temp);
- % Reconstruct data in serial form
- QAM_data_out_4(i) = xxx(1+(i-1)/2);
- QAM_data_out_4(i+1) = yyy(1+(i-1)/2);
- end
-
- % Make decision between [-3, -1, 1, 3]
- for i = 1:data_length/2
- if QAM_data_out_4(i) >= 1, QAM_data_out_4(i) = 3;
- elseif QAM_data_out_4(i) >= 0, QAM_data_out_4(i) = 1;
- elseif QAM_data_out_4(i) >= -1, QAM_data_out_4(i) = -1;
- else QAM_data_out_4(i) = -3;
- end
- end
-
- % Convert 4 level data [-3, -1, 1, 3] back to polar data [-1, 1]
- QAM_data_out_pol = zeros(1,data_length); % "cheating"
- for i = 1:2:data_length
- switch QAM_data_out_4(1 + (i-1)/2)
- case -3
- QAM_data_out_pol(i) = -1;
- QAM_data_out_pol(i+1) = -1;
- case -1
- QAM_data_out_pol(i) = -1;
- QAM_data_out_pol(i+1) = 1;
- case 1
- QAM_data_out_pol(i) = 1;
- QAM_data_out_pol(i+1) = -1;
- case 3
- QAM_data_out_pol(i) = 1;
- QAM_data_out_pol(i+1) = 1;
- otherwise
- disp('Error detected in switch statment - This should not be happening.');
- end
- end
- QAM_data_out = pol2bin(QAM_data_out_pol); % convert back to binary
-
- % Stop stopwatch to calculate how long QAM simulation takes
- QAM_simulation_time = toc;
- if QAM_simulation_time > 60
- disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time/60), ' minutes.'));
- else
- disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time), ' seconds.'));
- end
- end