Creating sounds with changing amplitude
조회 수: 31 (최근 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!