I need help combining two sine waves

조회 수: 45 (최근 30일)
Blake
Blake 2024년 6월 17일
댓글: Image Analyst 2024년 6월 17일
Hello. I am trying to learn how to end an ascending sine wave at the start of a signal and end a descending sine wave at the end of a signal. Here is what I have so far:
f=1000;
n=10;
T=1/f;
t=(0:T/100:n*T);
s = sin(2*pi*t*f);
e = (-expm1(-t*250));
e2 = (exp(-t*250));
n = s .* e;
m = e2 .* s;
fix = n + s + m;
plot(t,fix);
I am still doing research on how to properly add them but any help from others would be appreciated.

채택된 답변

Blake
Blake 2024년 6월 17일
After further coding I solved my question using this:
f=1000;
n=10;
T=1/f;
t=(0:T/100:n*T);
s = sin(2*pi*t*f);
e = (-expm1(-t*500));
e2 = (exp(-t*300));
tn = linspace(0,5*T,1001);
n = s .* e;
ts = linspace(5*T,8*T,1001);
tm = linspace(8*T,10*T,1001);
m = e2 .* s;
plot([tn ts tm],[n s m]);
However, if there is a way to clean this up I would be interested.

추가 답변 (2개)

Image Analyst
Image Analyst 2024년 6월 17일
How about using cos() instead of sin() and adjusting your ending time:
f=1000;
n=10;
T=1/f;
t=(0:T/100:n*T - T/2);
s = cos(2*pi*t*f);
e = (-expm1(-t*250));
e2 = (exp(-t*250));
n = s .* e;
m = e2 .* s;
y = n + s + m;
plot(t, y, 'LineWidth', 3);
grid on;
Also, fix is the name of a built-in function so you should not use it as the name of your variable.
If this is not what you want then supply a screenshot of what you want the plot to look like.
  댓글 수: 3
Blake
Blake 2024년 6월 17일
I believe my question might not have been worded properly. I want to have one end grow to a frequency and the other end decay to zero. Like a siren growing loud and then decaying. I added a picture with what I did. I added an answer to this on the question. If there is a way to clean up the code I would be open. Thank you for helping!
Image Analyst
Image Analyst 2024년 6월 17일
If it does what you want, I'm not going to mess with it. It's only a few lines of code. the only thing I would do it to add comments and make your variable names long and descriptive. Right now it's looks like an alphabet soup of code and might be hard for some one else, or you later, to figure out what's going on.

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


Hassaan
Hassaan 2024년 6월 17일
f = 1000; % Frequency of sine wave in Hz
n = 10; % Number of periods to generate
T = 1 / f; % Period of sine wave in seconds
t = (0:T/100:n*T); % Time vector
% Generate sine wave
s = sin(2 * pi * f * t);
% Define envelope functions
riseTime = n / 10; % Time in seconds over which sine wave rises
fallTime = n / 10; % Time in seconds over which sine wave falls
% Envelope for rising part
riseEnvelope = min(1, t / riseTime);
% Envelope for falling part
fallEnvelope = min(1, (n * T - t) / fallTime);
% Combined envelope
envelope = min(riseEnvelope, fallEnvelope);
% Modulated sine wave
modulatedSine = s .* envelope;
% Plot the result
figure;
plot(t, modulatedSine);
xlabel('Time (s)');
ylabel('Amplitude');
title('Combined Ascending and Descending Sine Wave');
grid on;

카테고리

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

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by