Resampling a signal up and downsample

40 次查看(过去 30 天)
Jackson
Jackson 2021-12-8
回答: MULI 2024-2-21
Given the following signal: x(nT)= cos(2*pi *fc* nT)where fc=100KHz and (1/T )= 300, 000 samples per second. Resample x(nT) to a new rate of (6/5) times the original rate.
plot the time domain and frequency domain output at each sample rate change stage and the final spectrum output at the new sample rate
Does anyone know how to do this in the MATLAB script without using SIMULINK or the resample function? I only learned how to do this in the SIMULINK way.
So far I only got the upsample of 6 by doing this way, but got no idea how to make a filter and downsample.
fc=100000;
nT=linspace(1,2,300000);
x = cos(2*pi*fc*nT);
upsample=zeros(1,length(x)*6);
upsample(1:6:end)=x;

回答(1 个)

MULI
MULI 2024-2-21
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

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by