I want to generate a sine wave with decreasing amplitude after a fixed interval

조회 수: 36 (최근 30일)
Mariam Ali
Mariam Ali 2022년 3월 22일
댓글: Mariam Ali 2022년 3월 22일
Can someone tell me how to plot a sine wave which has an amplitude of 10 for 5 seconds. The amplitude then drops to 5 abd stays there from 5 to 10 seconds. After 10 sec to 15 seconds, the amplitude further drops to 3.

답변 (3개)

KSSV
KSSV 2022년 3월 22일
You may proceed something like below:
t = linspace(0,15,1000) ;
y = zeros(size(t)) ;
idx = t<=5 ;
y(idx) = 10*sin(t(idx)) ;
idx = t > 5 & t <= 10 ;
y(idx) = 5*sin(t(idx)) ;
idx = t > 10 ;
y(idx) = 3*sin(t(idx)) ;
plot(t,y)
  댓글 수: 1
Mariam Ali
Mariam Ali 2022년 3월 22일
Hi, I don't want a stationary wave. I want it to be shown dynamically. Is there a way to do that?

댓글을 달려면 로그인하십시오.


Burhan Burak AKMAN
Burhan Burak AKMAN 2022년 3월 22일
You can create in the for loop like that,
%We need a frequency for create sine wave I used 2hz you can change
freq=2;
%Angular velocity
w=2*pi*freq;
%Preallocation wave array by zeros for fast computing
wave=zeros(1,1000);
%Index for wave
i=1;
for t=linspace(0,15,1000)
if t<=5
amplitude=10;
elseif t<=10
amplitude=5;
elseif t<=15
amplitude=3;
end
wave(i)=amplitude*sin(w*t);
i=i+1;
end
plot(wave)
  댓글 수: 1
Mariam Ali
Mariam Ali 2022년 3월 22일
Hi, I don't want a stationary wave. I want it to be shown dynamically. Is there a way to do that?

댓글을 달려면 로그인하십시오.


Sam Chak
Sam Chak 2022년 3월 22일
편집: Sam Chak 2022년 3월 22일
Because the frequency of the wave is not specified and the decreasing amplitude is mentioned on the title, here is another solution contender, which offers a smooth function that produces a damped sinusoidal wave with the desired amplitudes at the specified time intervals:
Note: The equation is derived from the knowledge about the damped sine wave. The key is to formulate the desired exponential damping envelope at the specified time interval
where the parameters are given by:
  • is the amplitude at the initial time ,
  • is the amplitude at the final time ,
  • is the time interval.
% simulation settings
tStart = 0; % start time
tEnd = 20; % end time
stepSize = 0.01; % size between each element
numEl = length(tStart : stepSize : tEnd); % number of elements
% the equation
t = linspace(tStart, tEnd, numEl)'; % create evenly spaced number of elements over the interval [tStart, tEnd] on the x-axis
y = ((5*exp(-(log(5/3)/5)*(t - 5)) + 10*exp(-(log(2)/5)*t))/2 + ((5*exp(-(log(5/3)/5)*(t - 5)) - 10*exp(-(log(2)/5)*t))/2).*sign(t - 5)).*cos((2*pi/5)*t);
% Create a 2D line plot and specify the line color, line style, and line width.
plot(t, y, 'b-', 'LineWidth', 1.5);
grid on;
yticks([-10 -5 0 3 5 10]);
xlabel('t');
ylabel('y');
title('A Smooth Damped Sinusoidal Wave')
Result:
If you want to generate multiple peaks with the same amplitude in the specified time interval, then @Burhan Burak AKMAN's generalized solution (which is built on @KSSV's conditional logic idea) is the closest to what you seek. However, the conditional logic may create discontinuities at the time interval when the period of the sinusoid is not the same as the length of the time interval.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by