Creating sounds with changing amplitude
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MARGARET
am 26 Jan. 2023
Kommentiert: Star Strider
am 6 Feb. 2023
I am trying to making sounds that simulate approaching noises. I want to create a 2 second 440 Hz sine wave that changes in amplitude (pure tone that gets louder over time). I need to get this change in amplitude two ways: 1) linearly where the change in amplitude is constant. And 2) where the change in amplitude is 1/((20*time)^2).
Is there a way to specify change in amplitude using equations? Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 26 Jan. 2023
Bearbeitet: Star Strider
am 26 Jan. 2023
Try this —
Fs = 44100;
Ft = 440;
sec = 2;
t = linspace(0, Fs*sec*60-1, Fs*sec*60).'/Fs;
% ChkFrq = 1/t(2)
s = sin(2*pi*Ft*t);
figure
plot(t, s)
grid
xlim([0 4410/Fs]) % 1 Seconds
xlabel('Time')
ylabel('Amplitude')
title('Original Signal')
s1 = s .* t/max(t); % Increases Linearly From 1 To Zero Over 't'
figure
plot(t, s1)
grid
xlim([0 4410/Fs]) % 1 Seconds
xlabel('Time')
ylabel('Amplitude')
title('Linearly Increasing Signal')
s2 = s .* 20./((t.^2)+t(2)); % This Envelope Will Clip
figure
plot(t, s2)
grid
xlim([0 4410/Fs]) % 1 Seconds
xlabel('Time')
ylabel('Amplitude')
title('Original Signal With Requested Envelope')
s3 = s .* (1/max(t)^2)/3./((t.^2)+t(2)); % This Envelope Will Not Clip
figure
plot(t, s3)
grid
xlim([0 4410/Fs]) % 1 Seconds
xlabel('Time')
ylabel('Amplitude')
title('Original Signal With Modified Envelope')
Fn = Fs/2;
L = numel(s);
NFFT = 2^nextpow2(L);
FTs = fft(s.*hann(L),NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTs(Iv))*2)
grid
xlim([400 500])
xlabel('Frequency (Hz)')
ylabel('Magnitude')
title('Fourier Transform')
These create column vectors, so use the sound function or the audioplayer function to listen to them.
EDIT — Corrected typographical errors.
.
4 Kommentare
Star Strider
am 6 Feb. 2023
As always, my pleasure!
The ‘s3’ code is essentially a normalised version of ‘s2’ so that it will not clip or distort.
‘If I wanted to change the 20 value to 15, how would I do that?’
Change that value, and then use the normalize function with it to keep it from clipping.
s3 = normalize(s2, 'range',[-1 1]);
and plotting that result in this version produces —
Fs = 44100;
Ft = 440;
sec = 2;
t = linspace(0, Fs*sec*60-1, Fs*sec*60).'/Fs;
% ChkFrq = 1/t(2)
s = sin(2*pi*Ft*t);
s2 = s .* 20./((t.^2)+t(2)); % This Envelope Will Clip
s3 = normalize(s2, 'range',[-1 1]); % Will Not Clip When 'normalize' Used First
figure
plot(t, s3)
grid
xlim([0 4410/Fs]) % 1 Seconds
xlabel('Time')
ylabel('Amplitude')
title('Original Signal With Requested Envelope, Normalized To [-1 1]')
to achieve the same result. The objective is to keep it from clipping the output, and distorting when played back.
.
Weitere Antworten (1)
Image Analyst
am 26 Jan. 2023
See attached demo where I vary both the pitch and amplitude of a sound waveform according to mathematical formulas.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Spectral Measurements 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!