Generating a pulse signal

17 views (last 30 days)
Can Kayan
Can Kayan on 2 Nov 2021
Commented: Star Strider on 2 Nov 2021
Hİ guys, I basically have this code and want to generate below figure. But I can't understand how it works. I tried the help function for the impulse but still couldn't understand it.
I only have hard time generating the pulse signal.
f=3; %frequency [Hz]
t0=(0:0.0009:1);
a=1; %amplitude [V]
phi=0; %phase
y=a*sin(2*pi*f*t0+phi)-2;
f=5; %frequency [Hz]
t1=(2:0.0009:3);
a=1; %amplitude [V]
phi=0; %phase
z=a*cos(2*pi*f*t1+phi)+2;
t = (1:0.01:2);
impulse = t==1;
impulse1 = t==1.1;
impulse2 = t==1.2;
impulse3 = t==1.3;
impulse4 = t==1.4;
impulse5 = t==1.5;
impulse6 = t==1.6;
impulse7 = t==1.7;
impulse8 = t==1.8;
impulse9 = t==1.9;
impulse10 = t==2;
x=double(impulse).*1;
x1=double(impulse1).*1;
x2=double(impulse2).*1;
x3=double(impulse3).*1;
x4=double(impulse4).*1;
x5=double(impulse5).*1;
x6=double(impulse6).*1;
x7=double(impulse7).*1;
x8=double(impulse8).*1;
x9=double(impulse9).*1;
x10=double(impulse10).*1;
d=linspace(0,3,3335);
c=[y x x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 z];
subplot(4,1,1)
plot(d,c,'black','LineWidth',1.5)
  1 Comment
Can Kayan
Can Kayan on 2 Nov 2021
above code works but i cant understand how it does. That's the part I need help with

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 2 Nov 2021
The posted code is an extremely cumbersome way of coding an extremely straightforward problem.
The ‘fcn’ function here should make it more understandable —
t = linspace(0, 3, 1800);
f=3; %frequency [Hz]
a=1; %amplitude [V]
phi=0; %phase
% y=a*sin(2*pi*f*t0+phi)-2
fcn = @(t,f,a,phi) (a*sin(2*pi*f*t+phi)-2) .* ((t>=0) & (t<1)) + (rem(t,0.1)<=1E-2) .* ((t>=1) & (t<2)) + (a*sin(2*pi*f*t+phi)+2) .* ((t>=2) & (t<=3));
% ↑ ← FIRST 'sin' CALL ↑ ← PULSE TRAIN ↑ ← SECOND 'sin' CALL
figure
plot(t, fcn(t,f,a,phi))
grid
.
  5 Comments
Jon
Jon on 2 Nov 2021
Edited: Jon on 2 Nov 2021
Also in the OP's post the frequency for the two sinusoids is different, but only one frequency is used in the anonymous function. You would just need to modify the anonymous function a little if you actually need two different frequencies
Star Strider
Star Strider on 2 Nov 2021
Corrected ‘fcn’ and plot —
t = linspace(0, 3, 3335);
f=[3 5]; %frequency [Hz]
a=1; %amplitude [V]
phi=0; %phase
fcn = @(t,f,a,phi) (a*sin(2*pi*f(1)*t+phi)-2) .* ((t>=0) & (t<1)) + (rem(t,0.1)<=1E-2) .* ((t>=1) & (t<2)) + (a*cos(2*pi*f(2)*t+phi)+2) .* ((t>=2) & (t<=3));
% ↑ ← 'sin' CALL ↑ ← PULSE TRAIN ↑ ← 'cos' CALL
figure
plot(t, fcn(t,f,a,phi))
grid
NOTE — The original ‘z’ is coded as cos, however plotted as sin. This cannot be accounted for by ‘phi’ that is 0 in both function calls. The first value of ‘z’ in the original code is 3 and not 2 as plotted.
.

Sign in to comment.


Jon
Jon on 2 Nov 2021
Edited: Jon on 2 Nov 2021
I like @Star Strider's approach, but as I had already coded up the approach below before I saw that, I thought I would provide this just to show an alternative approach.
% define first sinusoidal segment
tStart = 0;
tEnd = 1;
f=3; %frequency [Hz]
a=1; %amplitude [V]
phi=0; %phase
t1 = tStart:0.0009:tEnd;
x1=a*sin(2*pi*f*t1+phi)-2;
% define impulses
tStart = 1;
tEnd = 2;
t2 = tStart:0.001:tEnd;
idx = 1:100:1000; % indices in t2 where impulses occur
impulseMag = 1; % magnitude of impulses
% assign impulses at corresponding times where they occur
x2 = zeros(size(t2)); % initialize with no impulses
x2(idx) = impulseMag; % assign impulses
% define second sinusoidal segment
tStart = 2;
tEnd = 3;
f=5; %frequency [Hz]
a=1; %amplitude [V]
phi=0; %phase
t3 = tStart:0.0009:tEnd
x3=a*cos(2*pi*f*t3+phi)+2;
% make overall signal
t = [t1 t2 t3];
x = [x1 x2 x3];
% plot results
figure
subplot(4,1,1)
plot(t,x,'black','LineWidth',1.5)

Categories

Find more on Simulink in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!