How do I generate sound with durations and pause in between?
조회 수: 20 (최근 30일)
이전 댓글 표시
Dear all,
I attemp to generate sound (pure tone) in different duration (50, 100, 200, 400, 800 ms) with 1s rest (no sound) in between. Is it possible to do it in mathlab, or which program do you suggest me use to generate this? Thank you all!!!
댓글 수: 0
답변 (2개)
Walter Roberson
2021년 2월 8일
편집: Walter Roberson
2021년 2월 8일
Fs = 9681; %Sampling frequency, Hz
Tf = 473; %tone frequency, Hz
silence = @() zeros(Fs,1);
ms2Samp = @(duration) floor(duration/1000*Fs);
tone = @(duration) sinpi(2 .* Tf .* (0:ms2Samp(duration)-1)).';
out = [tone(50); silence(); tone(100); silence(); tone(400); silence(); tone(800)];
sound(out, Fs)
댓글 수: 0
Mathieu NOE
2021년 2월 8일
hello
this is a demo to generate beeps with pause between
%
file = 'test.wav' ;
f = 500; % f is the fundamental frequency in Hz
d_on = 3; % d_on is the duration in seconds with signal
d_off = 1; % d_off is the duration in seconds without signal (pause)
repeat = 25; % how many times the pattern must be replicated
p = [0.8,0.8,0.8,0.8,0.8];
synthesize(file,f,d_on,d_off,p,repeat);
function synthesize(file,f,d_on,d_off,p,repeat)
% Matlab function synthesize(file,f,d,p)
% creates a .wav audio file of a sound where the fundamental frequency
% and amplitudes(power) of the harmonics may be specified.
%
% file is a string which is the name of the .wav file.
% f is the fundamental frequency in Hz
% d_on is the duration in seconds with signal
% d_off is the duration in seconds without signal (pause)
% p is a length n vector of amplitudes
% repeat defines how many times the pattern must be replicated
% Mark R. Petersen, U. of Colorado Boulder Applied Math Dept, Feb 2004
% mod by M Noé Jan 2021
Fs=22050; nbits=8; % frequency and bit rate of wav file
t_on = (0:1/Fs:d_on-1/Fs);
y = zeros(1,Fs*d_on); % initialize sound data
for n=1:length(p)
y = y + p(n)*sin(2*pi*n*f*t_on); % sythesize waveform
end
y = y(:);
% % apply window to smooth start / stop ends of signal (optionnal)
% w = hanning(length(y));
% y = y.*w;
y = .95*y/max(y); % normalize. Coefficent controls volume.
% padd signal with zeros of duration t_off
y = [y; zeros(Fs*d_off,1)];
% now repeat this pattern multiple times
y_out = [];
for ci = 1:repeat
y_out = [y_out; y];
end
% plot (optionnal)
plot(y_out);grid
title('Waveform');
xlabel('samples');
ylabel('amplitude');
audiowrite(file,y_out, Fs,'BitsPerSample', nbits)
end
댓글 수: 2
PREETHAM
2024년 2월 18일
Hey how can I generate sound continuously with any gap(no sound) in between, the sound should continue generating until I press the stop button
Walter Roberson
2024년 2월 18일
For that, use the audio device writer from the Audio System Toolbox; https://www.mathworks.com/help/audio/ref/audiodevicewriter-system-object.html
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!