I need help combining two sine waves

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.

 Risposta accettata

Blake
Blake il 17 Giu 2024
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.

Più risposte (2)

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 Commenti

You can also add a phase to shift the sine wave, if you'd rather do it that way.
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!
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.

Accedi per commentare.

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;

Prodotti

Release

R2019a

Tag

Richiesto:

il 17 Giu 2024

Commentato:

il 17 Giu 2024

Community Treasure Hunt

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

Start Hunting!

Translated by