Create a function that generates different periodic signals

This is what I tried so far, but my solution doesnt seen to be working, in a plot it gives me 4 straight lines. Can someone help me find my mistake ? Thank you so so much!
function[f]=signal(s,t,A,M,T,N)
switch s
case 'Carre'
for n=1:N
S=S+(((4*A)/(N*pi))*sin((N*pi)/2)*cos(((2*N*pi)*t)./T));
end
f=M+S;
case 'Triangle'
for n=1:N
S=S+(((8*A)/(N^2*pi^2))*cos(((2*N*pi)*t)./T));
end
f=M+S;
case 'Scie'
for n=1:N
S=S+(((-1)^(N+1))*((2*A)/(N*pi))*sin(((2*N*pi)*t)./T));
end
f=M+S;
case 'Cosinus'
for n=1:N
S=S+(A*cos(((2*N*pi)*t)./T));
end
f=M+S;
otherwise
disp('Error. Name of the signal incorrectly entered.')
end
end

1 件のコメント

Voss
Voss 2022 年 2 月 22 日
Try changing N to n inside the for loops.

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

 採用された回答

KSSV
KSSV 2022 年 2 月 22 日
編集済み: KSSV 2022 年 2 月 22 日

0 投票

I will show one, you may try the others. Check the code though. Is the plot looks like is what you expected?
t = 0:0.01:10 ; % time
A = 2 ;
M = 1; % MEan value
N = 10 ;
n = 1:N ;
T = 4 ;
nt = length(t) ;
F = zeros(nt,1) ;
for i = 1:nt
f = 4*A./(n*pi).*sin(n*pi/2).*cos(2*n*pi*t(i)/T) ;
F(i) = M+sum(f) ;
end
plot(t,F)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2022 年 2 月 22 日

編集済み:

2022 年 3 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by