Copy the MATLAB Code from here
MATLAB Code
clc;
clear;
close all;
% Define a bit sequence
bitSeq = [0, 1, 0, 0, 1, 1, 1, 0, 0, 1];
% Perform MSK modulation
[modSignal, timeVec] = modulateMSK(bitSeq, 10, 10, 10000);
% Plot the modulated signal
subplot(2,1,1);
samples = 1:numel(bitSeq);
stem(samples, bitSeq);
title('Original message signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Plot the modulated signal
subplot(2,1,2);
samples = 1:10000;
plot(samples / 10000, modSignal(1:10000));
title('MSK modulated signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform MSK demodulation
demodBits = demodMSK(modSignal, 10, 10, 10000);
% Function to perform MSK modulation
function [signal, timeVec] = modulateMSK(bits, carrierFreq, baudRate, sampleFreq)
% Converts a binary bit sequence into an MSK-modulated signal
% Inputs:
% bits - Binary input sequence
% carrierFreq - Carrier frequency
% baudRate - Symbol rate
% sampleFreq - Sampling frequency
% Outputs:
% signal - Modulated MSK signal
% timeVec - Corresponding time vector
% Convert bits to NRZ format (-1, 1)
diffEncBits = 2 * bits - 1;
diffEncBits = [-1, diffEncBits]; % Append initial value
% Define time parameters
numBits = length(bits);
symbDur = 1 / baudRate;
timeVec = 0:1/sampleFreq:numBits * symbDur - (1/sampleFreq);
% Compute phase shifts
phaseShift = zeros(1, numBits + 1);
for idx = 2:numBits+1
phaseShift(idx) = mod(phaseShift(idx-1) + ((pi * idx) / 2) * (diffEncBits(idx-1) - diffEncBits(idx)), 2 * pi);
end
phaseShift = phaseShift(2:end);
diffEncBits = diffEncBits(2:end);
% Generate MSK waveform
symbolIdx = floor(timeVec / symbDur) + 1;
signal = cos(2 * pi * (carrierFreq + diffEncBits(symbolIdx) / (4 * symbDur)) .* timeVec + phaseShift(symbolIdx));
end
% Function to perform MSK demodulation
function bitSeq = demodMSK(signal, carrierFreq, baudRate, sampleFreq)
% Recovers a binary bit sequence from an MSK-modulated signal
% Inputs:
% signal - MSK-modulated input signal
% carrierFreq - Carrier frequency
% baudRate - Symbol rate
% sampleFreq - Sampling frequency
% Outputs:
% bitSeq - Demodulated binary sequence
symbDur = 1 / baudRate;
samplesPerSymbol = round(symbDur * sampleFreq);
numSamples = length(signal);
% Generate reference MSK waveforms for bits 0 and 1
refWave1 = modulateMSK([1], carrierFreq, baudRate, sampleFreq);
refWave0 = modulateMSK([0], carrierFreq, baudRate, sampleFreq);
bitSeq = logical.empty;
% Demodulation using correlation
for startIdx = 1:samplesPerSymbol:numSamples
if startIdx + samplesPerSymbol > numSamples
break;
end
sampleSegment = signal(startIdx:startIdx+samplesPerSymbol-1);
% Compute cross-correlation with reference waveforms
corr1 = xcorr(sampleSegment, refWave1);
corr0 = xcorr(sampleSegment, refWave0);
% Compare correlation values to determine bit
if max(corr1) + abs(min(corr1)) > max(corr0) + abs(min(corr0))
bitSeq = [bitSeq, 1];
else
bitSeq = [bitSeq, 0];
end
end
end
clear;
close all;
% Define a bit sequence
bitSeq = [0, 1, 0, 0, 1, 1, 1, 0, 0, 1];
% Perform MSK modulation
[modSignal, timeVec] = modulateMSK(bitSeq, 10, 10, 10000);
% Plot the modulated signal
subplot(2,1,1);
samples = 1:numel(bitSeq);
stem(samples, bitSeq);
title('Original message signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Plot the modulated signal
subplot(2,1,2);
samples = 1:10000;
plot(samples / 10000, modSignal(1:10000));
title('MSK modulated signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform MSK demodulation
demodBits = demodMSK(modSignal, 10, 10, 10000);
% Function to perform MSK modulation
function [signal, timeVec] = modulateMSK(bits, carrierFreq, baudRate, sampleFreq)
% Converts a binary bit sequence into an MSK-modulated signal
% Inputs:
% bits - Binary input sequence
% carrierFreq - Carrier frequency
% baudRate - Symbol rate
% sampleFreq - Sampling frequency
% Outputs:
% signal - Modulated MSK signal
% timeVec - Corresponding time vector
% Convert bits to NRZ format (-1, 1)
diffEncBits = 2 * bits - 1;
diffEncBits = [-1, diffEncBits]; % Append initial value
% Define time parameters
numBits = length(bits);
symbDur = 1 / baudRate;
timeVec = 0:1/sampleFreq:numBits * symbDur - (1/sampleFreq);
% Compute phase shifts
phaseShift = zeros(1, numBits + 1);
for idx = 2:numBits+1
phaseShift(idx) = mod(phaseShift(idx-1) + ((pi * idx) / 2) * (diffEncBits(idx-1) - diffEncBits(idx)), 2 * pi);
end
phaseShift = phaseShift(2:end);
diffEncBits = diffEncBits(2:end);
% Generate MSK waveform
symbolIdx = floor(timeVec / symbDur) + 1;
signal = cos(2 * pi * (carrierFreq + diffEncBits(symbolIdx) / (4 * symbDur)) .* timeVec + phaseShift(symbolIdx));
end
% Function to perform MSK demodulation
function bitSeq = demodMSK(signal, carrierFreq, baudRate, sampleFreq)
% Recovers a binary bit sequence from an MSK-modulated signal
% Inputs:
% signal - MSK-modulated input signal
% carrierFreq - Carrier frequency
% baudRate - Symbol rate
% sampleFreq - Sampling frequency
% Outputs:
% bitSeq - Demodulated binary sequence
symbDur = 1 / baudRate;
samplesPerSymbol = round(symbDur * sampleFreq);
numSamples = length(signal);
% Generate reference MSK waveforms for bits 0 and 1
refWave1 = modulateMSK([1], carrierFreq, baudRate, sampleFreq);
refWave0 = modulateMSK([0], carrierFreq, baudRate, sampleFreq);
bitSeq = logical.empty;
% Demodulation using correlation
for startIdx = 1:samplesPerSymbol:numSamples
if startIdx + samplesPerSymbol > numSamples
break;
end
sampleSegment = signal(startIdx:startIdx+samplesPerSymbol-1);
% Compute cross-correlation with reference waveforms
corr1 = xcorr(sampleSegment, refWave1);
corr0 = xcorr(sampleSegment, refWave0);
% Compare correlation values to determine bit
if max(corr1) + abs(min(corr1)) > max(corr0) + abs(min(corr0))
bitSeq = [bitSeq, 1];
else
bitSeq = [bitSeq, 0];
end
end
end
Output
In Minimum Shift Keying (MSK), the two frequencies used for 0 and 1 depend on the carrier frequency \( f_c \) and the baud rate \( R_b \) (symbols per second).
Formula for MSK frequencies:
The two frequencies are given by:
\[ f_0 = f_c - \frac{1}{4T} \] \[ f_1 = f_c + \frac{1}{4T} \]where \( T = \frac{1}{\text{baud rate}} \) is the symbol duration.
Given values:
- Carrier frequency: \( f_c = 10 \) Hz
- Baud rate: \( R_b = 10 \) symbols/sec
- Symbol duration: \( T = \frac{1}{10} = 0.1 \) sec
Now, calculating the frequencies:
\[ f_0 = 10 - \frac{1}{4 \times 0.1} = 10 - \frac{1}{0.4} = 10 - 2.5 = 7.5 \text{ Hz} \] \[ f_1 = 10 + \frac{1}{4 \times 0.1} = 10 + 2.5 = 12.5 \text{ Hz} \]
Minimum Shift Keying (MSK) Simulator
Differences Between MSK and FSK
In FSK, if bits change from 0 to 1, and
f₀ ≠ f₁
, the carrier switches frequency — but phase continuity is not maintained unless explicitly enforced. This causes a sudden jump in phase at the bit boundary. In MSK, the phase is not static or abruptly switching. It evolves linearly over time based on the bit value, ensuring continuity. For bit duration Tb
, the frequency deviation is: Δf = ±(1 / 4Tb)
[Read More in Detail ...]