How to create siren sound using makesound

11 vues (au cours des 30 derniers jours)
Pierre-Alexandre Blot
Pierre-Alexandre Blot le 1 Fév 2021
Commenté : Mathieu NOE le 2 Fév 2021
Dear all,
I am trying to generate sound which follows an amplitude signal. Means, when amplitude of my signal is increasing, my frequency is too, etc ..
To start as simple, I made different consecutive sounds with different frequencies to first understand the principle, playing well. Then increased the complexity by generating a vector which represents the frequency variation of my sound. I did it as sinusoidal, should sound like a siren.
Then using this variable frequency vector, I generate the sound using the sound function. At the end, I can hear the frequency varying but also constantly increasing and I don't understand why. Could you please tell me what is wrong here ? I am pretty sure I'm facing very basic misunderstanding ...
Thank you in advance !
% create the frequency oscillation "siren" frequency, between 200 and 400Hz, oscillating at 1Hz.
time = 0:0.001:5;
f = 100*sin(2*pi*1*time) + 300;
fs=44100; % sample rate
% setup the nb of points according to time length of the signal and sample
% rate
d=time(end); %duration
n=fs*d; % number of points
t=(1:n)/fs;
% Interp the previous frequency vector to fit the new time vector
f = interp1(time,f,t);
% Generate the signal
y=sin(2*pi*f.*t);
sound(y,fs);

Réponse acceptée

Mathieu NOE
Mathieu NOE le 1 Fév 2021
hello
You have to remember that the output is the sinus of an angle, which is the integral of frequency over time;
so when frequency also varies with time , it's incorrect to simply make the product of 2*pi*f*t, you have to do the integral with cumtrapz as shown below :
I also simplified a bit your code (only one time vector needed)
fs=44100; % sample rate
dt = 1/fs;
t = 0:dt:5;
f = 100*sin(2*pi*1*t) + 300;
% Generate the signal
angl = cumtrapz(t,f); % angle is integral of frequency
y=sin(2*pi*angl);
sound(y,fs);
  2 commentaires
Pierre-Alexandre Blot
Pierre-Alexandre Blot le 2 Fév 2021
Thank you so much ! It's now working and I could even go further in what I wanted to do (frequency following a given input signal). Thank you again for explanations ;)
Mathieu NOE
Mathieu NOE le 2 Fév 2021
my pleasure !
would you accept my answer ?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Measurements and Spatial Audio dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by