Animated Discrete Stem Plot

6 views (last 30 days)
Amritpal Kaur
Amritpal Kaur on 10 Nov 2016
Edited: Samayochita on 10 Feb 2025 at 4:40
So I want to create an animated plot of a discrete-time complex exponential function. The simplest non-animated plot would be given by this:
n=-5:40;
x=(exp((3*4j)*n)).*(n>=0);
y=real(x);
subplot(2,1,1);
stem (n,y)
z=imag(x);
subplot(2,1,2);
stem (n,z)
How do I animate it to show the function for the different numbers of samples considered in a given interval (assuming I have a time interval specified by start second and end second and a vector containing the number of sample values in the given interval)?
I tried along these lines:
figure,hold on
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
stem (k,y) %// Choose your own marker here
pause(0.001);
end
That doesn't compile. How to achieve this? Thanks.

Answers (1)

Samayochita
Samayochita on 10 Feb 2025 at 4:38
Edited: Samayochita on 10 Feb 2025 at 4:40
Hi Amritpal,
You're on the right track with animating a discrete-time complex exponential function, but there are a few issues with your approach: stem(k, y) is incorrect because k is just an index, but y is an entire vector. You need to plot only up to k at each step. Your xlim and ylim need to be set correctly for the entire range of the signal and pause(0.001) might be too fast to notice the animation.
To properly animate the function over a specified time interval with a given number of samples, we introduce the following parameters:
  1. t_start and t_end → Define the time interval.
  2. num_samples_vector → A vector containing different numbers of samples for different intervals.
  3. Sampling step (Ts) → Determined by the number of samples in a given interval.
The modified code is as follows:
% Define time interval and number of samples
start_time = 0; % Start second
end_time = 2; % End second
num_samples_vector = [10, 20, 30, 50]; % Different numbers of samples
% Loop over different sample sizes
for num_samples = num_samples_vector
% Define discrete time indices based on num_samples
n = linspace(-5, 40, num_samples); % Adjust sample points
% Compute the discrete-time complex exponential
x = (exp((3*4j)*n)) .* (n >= 0);
y = real(x);
z = imag(x);
% Create figure
figure;
% Real Part Plot
subplot(2,1,1);
hold on;
xlim([min(n) max(n)]);
ylim([min(y) max(y)]);
title(['Real Part of x[n] with ', num2str(num_samples), ' samples']);
xlabel('n');
ylabel('Amplitude');
grid on;
real_stem = stem(n(1), y(1), 'b', 'filled'); % Initial stem plot
% Imaginary Part Plot
subplot(2,1,2);
hold on;
xlim([min(n) max(n)]);
ylim([min(z) max(z)]);
title(['Imaginary Part of x[n] with ', num2str(num_samples), ' samples']);
xlabel('n');
ylabel('Amplitude');
grid on;
imag_stem = stem(n(1), z(1), 'r', 'filled'); % Initial stem plot
% Compute animation duration based on the time interval
total_time = end_time - start_time;
frame_delay = total_time / num_samples; % Time per sample
% Animation loop
for k = 1:length(n)
% Update real part plot
set(real_stem, 'XData', n(1:k), 'YData', y(1:k));
% Update imaginary part plot
set(imag_stem, 'XData', n(1:k), 'YData', z(1:k));
pause(frame_delay); % Control animation speed
end
end
Hope this helps!

Categories

Find more on Animation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!