Creating sounds with changing amplitude
34 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2023-1-26
编辑:Star Strider
2023-1-26
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 个评论
Star Strider
2023-2-6
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.
.
更多回答(1 个)
Image Analyst
2023-1-26
See attached demo where I vary both the pitch and amplitude of a sound waveform according to mathematical formulas.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!