How to plot this square signal without sum?

2 ビュー (過去 30 日間)
Armin
Armin 2023 年 7 月 7 日
編集済み: Angelo Yeo 2023 年 7 月 7 日
  3 件のコメント
Armin
Armin 2023 年 7 月 7 日
N = 1000;
vec1 = [];
vec2 = [];
tau = linspace(-1/2, 1/2, 1000);
for n = 1:2:N
vec1 = [vec1, 1/n];
vec2 = [vec2; sin(2 * n * pi * tau)];
end
plot(tau, vec1 * vec2)
But I don't know how to add the 4 / pi
Angelo Yeo
Angelo Yeo 2023 年 7 月 7 日
編集済み: Angelo Yeo 2023 年 7 月 7 日
I have already responded in the reddit post below.
https://www.reddit.com/r/matlab/comments/14sxpg2/how_to_plot_this_square_signal_without_sum/

サインインしてコメントする。

採用された回答

N A POORNA CHANDRA
N A POORNA CHANDRA 2023 年 7 月 7 日
hi Armin, here is the code to plot this square signal without sum
t = -0.5:0.001:0.5; % Time vector
f = zeros(size(t)); % Initialize the signal
for n = 1:2:10 % Iterate over odd values of n
f = f + (1/n) * sin(2*pi*n*t);
end
f = (4/pi) * f; % Scale the signal
plot(t, f);
xlabel('t');
ylabel('f(t)');
title('Square Signal');
grid on;

その他の回答 (1 件)

Swapnil Tatiya
Swapnil Tatiya 2023 年 7 月 7 日
Hi @Armin,
You could use the heaviside function of the symbolic variable toolbox,I'm attaching a link for your reference:
The link to syms is:
The link to subs is:
The following code snippet shows how you could use the above function:
% t1 and t2 are the limits as specified in the question
t2=0.5;
t1=-0.5;
%domain is kept a little wider so that the signal can be analysed with a
%little more ease
domain=t1-1:0.01:t2+1;
%syms is used to generate a symbolic variable,refer to the above link for any
%clarifications
syms t;
signal=heaviside(t+t2)-heaviside(t+t1);
%subs is used to substitute range of values into symbolic variables
y=subs(signal,t,domain);
plot(domain,y)
xlim([-2 2])
ylim([-1 2])
grid on
The output to the above code is the following:
Hope this helps!

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by