MATLAB Code
% Example: OFDM with Passband Transmission, Data Rate Calculation, and Signal Visualizationclear;
clc;
% 1. Define bitstream (as a string of bits)
bitstream = '1011000110110001'; % Example bitstream
% 2. Parameters
numSymbols = length(bitstream) / 2; % Each QPSK symbol maps to 2 bits
numSubcarriers = 4; % Number of subcarriers
nSymbolsPerOFDM = numSubcarriers; % Number of symbols per OFDM symbol (each subcarrier gets 1 symbol)
% Carrier Frequency (Passband)
f_c = 2e6; % Carrier frequency (in Hz) for passband transmission, e.g., 2 MHz
% Sampling Frequency
fs = 20e6; % Sampling frequency (in Hz), e.g., 20 MHz
% Symbol duration (inverse of symbol rate)
symbolDuration = numSubcarriers / fs; % Duration of one OFDM symbol
% 3. Map bits to QPSK symbols (2 bits -> 1 symbol)
qpskSymbols = zeros(1, numSymbols);
for i = 1:numSymbols
bits = bitstream(2*i-1:2*i); % Get 2 bits for each symbol
if strcmp(bits, '00')
qpskSymbols(i) = 1 + 1i;
elseif strcmp(bits, '01')
qpskSymbols(i) = 1 - 1i;
elseif strcmp(bits, '10')
qpskSymbols(i) = -1 + 1i;
elseif strcmp(bits, '11')
qpskSymbols(i) = -1 - 1i;
end
end
% 4. Reshape the symbols into groups of 4 (each group corresponds to one OFDM symbol)
ofdmSymbols = reshape(qpskSymbols, numSubcarriers, []);
% 5. Apply IFFT (Inverse Fast Fourier Transform) to convert from frequency domain to time domain
timeDomainSymbols = ifft(ofdmSymbols, numSubcarriers);
% 6. Add Cyclic Prefix (CP) - Take the last N samples of each symbol and prepend them
cyclicPrefixLength = 1; % Length of the cyclic prefix (for simplicity, we use 1)
ofdmWithCP = [timeDomainSymbols(end-cyclicPrefixLength+1:end, :); timeDomainSymbols];
% 7. Serialize (Concatenate) the OFDM symbols with CP
transmittedSignal_baseband = ofdmWithCP(:)'; % Concatenate all symbols with CP into one continuous signal
% 8. Passband Modulation (Shift to Passband using Carrier)
t_baseband = (0:length(transmittedSignal_baseband)-1) / fs; % Time vector for baseband signal
I = real(transmittedSignal_baseband) .* cos(2*pi*f_c*t_baseband); % In-phase component
Q = imag(transmittedSignal_baseband) .* sin(2*pi*f_c*t_baseband); % Quadrature component
transmittedSignal_passband = I + Q; % Passband signal
% 9. Time Scaling for Better Visualization
% For visualization purposes, we'll rescale the time axis to match the proper timing
t_upsampled = linspace(0, (length(transmittedSignal_baseband)-1)/fs, length(transmittedSignal_baseband));
t_passband = linspace(0, (length(transmittedSignal_baseband)-1)/fs, length(transmittedSignal_passband));
% 10. Display the results
figure;
% Plot the original message signal (bitstream converted to QPSK symbols)
subplot(3,1,1);
stem(real(transmittedSignal_baseband), 'filled', 'MarkerSize', 2);
hold on;
stem(imag(transmittedSignal_baseband), 'filled', 'MarkerSize', 2);
title('Original Message Signal (Baseband QPSK)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('In-phase', 'Quadrature');
grid on;
% Plot the baseband modulated signal
subplot(3,1,2);
plot(real(transmittedSignal_baseband), 'b');
hold on;
plot(imag(transmittedSignal_baseband), 'r');
title('Baseband Modulated Signal (QPSK)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('In-phase', 'Quadrature');
grid on;
% Plot the passband signal (shifted to carrier frequency)
subplot(3,1,3);
plot(t_passband, transmittedSignal_passband, 'k');
title('Passband OFDM Signal (with Carrier Modulation)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% 11. Data rate calculation
bitsPerSymbol = 2; % QPSK uses 2 bits per symbol
symbolRate = 1 / symbolDuration; % Symbol rate (in symbols per second)
% Data rate in bits per second (bps)
dataRate = numSubcarriers * bitsPerSymbol * symbolRate;
% Convert to Mbps (megabits per second)
dataRateMbps = dataRate / 1e6;
disp(['Data Rate: ', num2str(dataRateMbps), ' Mbps']);
Output
Data Rate: 40 Mbps