Hi Jackson,
I understand that you need to resample the signal using MATLAB script without using resample function. Here I am providing the code which performs the required resampling.
Firstly the signal is upsampled by a factor of 6 and using a low pass filter the signal is interpolated and then it is downsampled by a factor of 5.At the end I have plotted the original, upsampled, interpolated, and downsampled signals in the time domain and the frequency spectrum of the original and downsampled signals.
MATLAB Code:
clc;
clear all;
close all;
% Parameters
fc = 100000; % Carrier frequency in Hz
Fs = 300000; % Original sampling frequency in Hz
T = 1/Fs; % Original sampling period in seconds
duration = 1; % Duration of the signal in seconds
N = Fs * duration; % Number of samples
% Original signal
t = (0:N-1)*T; % Time vector for the original signal
x = cos(2*pi*fc*t); % Original signal
% Upsample by a factor of 6
L = 6; % Upsampling factor
x_up = zeros(1, N*L);
x_up(1:L:end) = x;
% Low-pass filter design
% The cutoff frequency should be at or below the Nyquist frequency of the original sampling rate
cutoff = Fs/2; % Low-pass filter cutoff frequency
order = 64; % Filter order
b = fir1(order, cutoff/(Fs*L/2)); % Design filter
% Compensate for the filter delay
delay = mean(grpdelay(b));
% Apply the filter to interpolate
x_interpolated = filter(b, 1, x_up);
% Compensate for the delay by shifting the signal
x_interpolated = [x_interpolated(delay+1:end), zeros(1, delay)];
% Downsample by a factor of 5
M = 5; % Downsampling factor
x_down = x_interpolated(1:M:end);
% Time vector for downsampled signal
t_down = (0:length(x_down)-1)*(T*L/M);
% Plot the signals and their spectra
figure;
% Original signal time domain
subplot(3,2,1);
plot(t, x);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Original signal frequency domain
subplot(3,2,2);
f = (-N/2:N/2-1)*(Fs/N);
X = fftshift(fft(x)/N);
plot(f, abs(X));
title('Original Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Upsampled signal time domain
subplot(3,2,3);
t_up = (0:length(x_up)-1)*(T/L);
stem(t_up(1:300), x_up(1:300), 'filled');
title('Upsampled Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Interpolated signal time domain
subplot(3,2,4);
plot(t_up, x_interpolated);
title('Interpolated Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Downsampled signal time domain
subplot(3,2,5);
stem(t_down(1:300), x_down(1:300), 'filled');
title('Downsampled Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Downsampled signal frequency domain
subplot(3,2,6);
N_down = length(x_down);
f_down = (-N_down/2:N_down/2-1)*(Fs*L/M/N_down);
X_down = fftshift(fft(x_down)/N_down);
plot(f_down, abs(X_down));
title('Downsampled Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Adjust plot layouts
subplot(3,2,1);
xlim([0, 5/fc]);
subplot(3,2,2);
xlim([-2*fc, 2*fc]);
subplot(3,2,3);
xlim([0, 5/fc]);
subplot(3,2,4);
xlim([0, 5/fc]);
subplot(3,2,5);
xlim([0, 5/fc]);
subplot(3,2,6);
xlim([-2*fc, 2*fc]);
You may refer these documentation links for the functions used in code

