Creating sounds with changing amplitude

32 ビュー (過去 30 日間)
MARGARET
MARGARET 2023 年 1 月 26 日
コメント済み: Star Strider 2023 年 2 月 6 日
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.

採用された回答

Star Strider
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 件のコメント
MARGARET
MARGARET 2023 年 2 月 6 日
In the code you first posted with s3, how did you derive that from 1/((20*time)^2)? If I wanted to change the 20 value to 15, how would I do that? Thanks!
Star Strider
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.
Using the normalize function, specifically:
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
Image Analyst 2023 年 1 月 26 日
See attached demo where I vary both the pitch and amplitude of a sound waveform according to mathematical formulas.

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by