FSK( frequency shift key) matlab code

Arvind Kumar
clc;
clear all;
close all;

fc = 1000;  % Carrier frequency in Hz
fs = 10000; % Sampling frequency in Hz
t = 0:1/fs:0.1; % Time vector from 0 to 0.1 seconds

data = [1 0 1 1 0 1];  % Binary data to be transmitted

% Calculate how many samples each bit represents
samples_per_bit = floor(length(t) / length(data));

% Initialize message signal
mt = []; 
for i = 1:length(data)
    % Repeat each bit according to samples per bit
    mt = [mt repmat(data(i), 1, samples_per_bit)]; 
end

% Adjust mt size to match the time vector length
if length(mt) > length(t)
    mt = mt(1:length(t));  % Truncate if necessary
end

% Generate carrier signal
carrier = sin(2 * pi * fc * (0:1/fs:(length(mt)-1)/fs));  % Sine wave for carrier signal

% PSK Modulation
psk = zeros(size(mt));  % Initialize PSK signal array
for i = 1:length(data)
    % Calculate start and end index for the current bit
    start_index = (i-1)*samples_per_bit + 1;
    end_index = start_index + samples_per_bit - 1;

    % Ensure end_index does not exceed the length of psk
    if end_index > length(psk)
        end_index = length(psk);
    end

    if data(i) == 1
        psk(start_index:end_index) = carrier(start_index:end_index);  % For bit '1'
    else
        psk(start_index:end_index) = -carrier(start_index:end_index);  % For bit '0'
    end
end

% Plotting
figure;
subplot(3,1,1);
plot(t, mt);
title('Message Signal (Data)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(3,1,2);
plot(t, carrier(1:length(t)));
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(3,1,3);
plot(t, psk);
title('PSK Modulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.