Creating sounds with changing amplitude

조회 수: 31 (최근 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 CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by