shift signal in time domain in matlab

To shift a signal in the time domain using Matlab, you can use the circshift() function.

The basic syntax for circshift function is B = circshift(A, K) where A is the input signal, K is the number of positions to shift and B is the shifted version of A.

Here is an example code block that shifts a sinusoidal signal by n samples to the right:

main.m
% Generate a sinusoidal signal
fs = 100; % Sampling rate
f = 5; % Frequency of the signal
t = 0:1/fs:1; % Time vector
x = sin(2*pi*f*t); % Sinusoidal signal

n = 10; % Number of samples to shift
x_shifted = circshift(x, n); % Shift the signal

% Plot the original and the shifted signal
plot(t, x, 'b-', t, x_shifted, 'r--');
legend('Original Signal', 'Shifted Signal');
xlabel('Time (s)');
ylabel('Amplitude');
412 chars
15 lines

In this code, we first generate a sinusoidal signal with a sampling rate of 100 Hz and a frequency of 5 Hz. We define a time vector using the sampling rate and the length of the signal.

We then shift the signal by 10 samples to the right using the circshift() function and store the result in x_shifted. Finally, we plot both the original and the shifted signal on the same graph using different line styles and add a legend to the plot.

You can modify the value of n to shift the signal by a different number of samples.

gistlibby LogSnag