MATLAB Code
clc;
clear;
close all;
% Configuration parameters
fs = 10000; % Sampling rate (Hz)
t = 0:1/fs:1-1/fs; % Time vector creation
% Signal definition
x = sin(2 * pi * 100 * t) + cos(2 * pi * 1000 * t);
% Calculate the Fourier Transform
y = fft(x);
z = fftshift(y);
% Create frequency vector
ly = length(y);
f = (-ly/2:ly/2-1) / ly * fs;
% Calculate phase while avoiding numerical precision issues
tol = 1e-6; % Tolerance threshold for zeroing small values
z(abs(z) < tol) = 0;
phase = angle(z);
% Plot the original Signal
figure;
subplot(3, 1, 1);
plot(t, x, 'b');
xlabel('Time (s)');
ylabel('|y|');
title('Original Messge Signal');
grid on;
% Plot the magnitude of the Fourier Transform
subplot(3, 1, 2);
stem(f, abs(z), 'b');
xlabel('Frequency (Hz)');
ylabel('|y|');
title('Magnitude of the Fourier Transform');
grid on;
% Plot the phase of the Fourier Transform
subplot(3, 1, 3);
stem(f, phase / pi, 'b');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase of the Fourier Transform');
grid on;
Output
Copy the MATLAB Code above from here
Another MATLAB Code
clc;
clear;
close all;
% Parameters
fs = 100; % Sampling frequency
t = 0:1/fs:1-1/fs; % Time vector
% Signal definition
x = cos(2*pi*15*t - pi/4) - sin(2*pi*40*t);
% Compute Fourier Transform
y = fft(x);
z = fftshift(y);
% Frequency vector
ly = length(y);
f = (-ly/2:ly/2-1)/ly*fs;
% Compute phase
phase = angle(z);
% Plot magnitude of the Fourier Transform
figure;
subplot(2, 1, 1);
stem(f, abs(z), 'b');
xlabel('Frequency (Hz)');
ylabel('|y|');
title('Magnitude of Fourier Transform');
grid on;
% Plot phase of the Fourier Transform
subplot(2, 1, 2);
stem(f, phase, 'b');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase of Fourier Transform');
grid on;
Output
Copy the MATLAB Code above from here
Further Reading