creating wav files that contain sequences of pulse bursts
4 views (last 30 days)
Show older comments
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
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 ?
Answers (1)
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
0 Comments
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!