step amplitude change in a sine wave code Matlab

조회 수: 3 (최근 30일)
Macyln Chingwena
Macyln Chingwena 2020년 7월 29일
답변: Macyln Chingwena 2020년 7월 29일
Hello. How do i create a step change in the amplitude of a sine wave. This is the code i have thus far:
I want to change the amplitude from 5 to say 10 after a few cycles like in the top fgure. Thanks for the help in advance.

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 7월 29일
Hi Macyln,
I am not exactly sure as what you are looking for. Based on the figure, i see that you wanted to have different levels of sine output at different times.
Here is a small script that can help you, if thats the case:
clear all
Ts = 1e-6;
fundamental_frequency = 50;
fundamental_period = 1/fundamental_frequency;
t = 0:Ts:(fundamental_period*10-Ts);
threshold = t(floor(length(t)/2)); % Half the cycles
% You can threshold with the time index directly or for 100 samples as such, use t(100)
index = t < threshold;
v_ref_A = zeros(size(t,1),1);
v_ref_A(index) = 5*sin(2*pi*fundamental_frequency*t(index));
v_ref_A(~index) = 10*sin(2*pi*fundamental_frequency*t(~index));
plot(t,v_ref_A)
Note, this can be extended with multiple thresholds. I have provided only for a half the length cycle.
Please let me know, if this is not something you are after.
Hope this helps.
Regards,
Sriram
  댓글 수: 1
Macyln Chingwena
Macyln Chingwena 2020년 7월 29일
Hello Sriram,
Thank you so much for the help. Yes thats exactly what i was trying to generate.
I guess i wasnt thinking properly but i als managed to produce the same out. In case someone else requires a similar waveform. l also managed to do it for multiple cycles.
Here is the code:
clear all
%Initialize variables
Ts_main = 1e-6;
fundamental_frequency = 50;
fundamental_period = 1/fundamental_frequency;
t = 0:Ts_main:(fundamental_period - Ts_main);
v1 = 5*sin(2*pi*fundamental_frequency*t);
v2 = 10*sin(2*pi*fundamental_frequency*t);
k = 1;
number_periods = 5;
v_ref_A = zeros(1,length(t)*number_periods);
for i = 1 : length(t)*number_periods
if i < length(t)*number_periods*9/20
v_ref_A(i) = v1(k);
else
v_ref_A(i) = v2(k);
end
if k < length(t)
k = k + 1;
else
k = 1;
end
end
Thank you once again foryour response.

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

추가 답변 (1개)

Macyln Chingwena
Macyln Chingwena 2020년 7월 29일
clear all
%Initialize variables
Ts_main = 1e-6;
fundamental_frequency = 50;
fundamental_period = 1/fundamental_frequency;
t = 0:Ts_main:(fundamental_period - Ts_main);
v1 = 5*sin(2*pi*fundamental_frequency*t);
v2 = 10*sin(2*pi*fundamental_frequency*t);
k = 1;
number_periods = 5;
v_ref_A = zeros(1,length(t)*number_periods);
for i = 1 : length(t)*number_periods
if i < length(t)*number_periods*9/20
v_ref_A(i) = v1(k);
else
v_ref_A(i) = v2(k);
end
if k < length(t)
k = k + 1;
else
k = 1;
end
end

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by