creating wav files that contain sequences of pulse bursts

14 views (last 30 days)
Dear MATLAB,
I am trying to make an audio wav file using MATLAB.
I wonder if it is possible to implement pulses whose pulse envelope has specific rise and fall time requirements in the range of 7.5 ms to 100 ms that are measured in acoustic domains.
I've tried the following code provided by an answer in the forum to create a single pulse:
% Sampling
fs = 1000; % Sampling rate [Hz]
Ts = 1/fs; % Sampling period [s]
fNy = fs / 2; % Nyquist frequency [Hz]
duration = 10; % Duration [s]
t = 0 : Ts : duration-Ts; % Time vector
noSamples = length(t); % Number of samples
% Original signal
x = 220.*sin(2 .* pi .* 50 .* t);
% Harmonics
x1 = 100.*sin(2 .* pi .* 100 .* t);
x2 = 100.*sin(2 .* pi .* 200 .* t);
x3 = 100.*sin(2 .* pi .* 300 .* t);
% Contaminated signal
xn = x + x1 + x2 + x3;
When I export it into a wav file, it comes out as a single beep.
Hence my second question is: how to make repetitive pulse bursts with a certain time interval (say a pulse burst, like a beep, every 1s)?
Thanks,
Yumeka
  5 Comments
Mathieu NOE
Mathieu NOE on 6 Jan 2021
hello
sure
do you have finally selected a code that gives you the basic pulse wave or are you still strugling with that ?
Mengjia Lyu
Mengjia Lyu on 6 Jan 2021
Thanks for the reply! I have the following code which I borrowed from Prof.Peterson's website for creating a single pulse:)
However, I would like to have a pulse train consisting of the pulse created by the code. Like (Beep-pause-Beep-pause-Beep...)
synthesize('test.wav',500,3,[0.8,0.8,0.8,0.8,0.8])
function synthesize(file,f,d,p)
% 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 is the duration in seconds
% p is a length n vector of amplitudes
%
% For example, synthesize('test.wav', 220, 3, [1 .8 .1 .04])
% makes a 3 second sample at 220 Hz with the harmonics shown.
%
% Mark R. Petersen, U. of Colorado Boulder Applied Math Dept, Feb 2004
Fs=22050; nbits=8; % frequency and bit rate of wav file
t = linspace(1/Fs, d, d*Fs); % time
y = zeros(1,Fs*d); % initialize sound data
for n=1:length(p);
y = y + p(n)*cos(2*pi*n*f*t); % sythesize waveform
end
y = .5*y/max(y); % normalize. Coefficent controls volume.
audiowrite( file,y, Fs,'BitsPerSample', nbits)
end

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 7 Jan 2021
hello
so this would be my suggestion , build on the existing code
you can uncomment the window section f it appeals to you
also some wave file players / editors can loop the file , so you are not obliged to create a huge wav file if you need the signal to play for one hour ; FYI, I use Goldwave that is pretty efficient but Audacity and others can do it too
%
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

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!