How do I generate sound with durations and pause in between?

47 views (last 30 days)
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!!!

Answers (2)

Walter Roberson
Walter Roberson on 8 Feb 2021
Edited: Walter Roberson on 8 Feb 2021
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)

Mathieu NOE
Mathieu NOE on 8 Feb 2021
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 Comments
PREETHAM
PREETHAM on 18 Feb 2024
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!