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

38 Ansichten (letzte 30 Tage)
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.

Antworten (3)

KSSV
KSSV am 22 Mär. 2022
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)

Burhan Burak AKMAN
Burhan Burak AKMAN am 22 Mär. 2022
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)

Sam Chak
Sam Chak am 22 Mär. 2022
Bearbeitet: Sam Chak am 22 Mär. 2022
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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by