How could I generate a biphasic pulse train?

hi :)
i m trying to generate a biphasic pulse train to modulate with speech signal. but all i found is pulse train with positive amplitude only.
how could I generate a biphasic pulse train ? any help ?

 Accepted Answer

hello
maybe this can help you
freq = 10 ;
Ts=1e-04;
t=0:Ts:1;
angl = 2*pi*(mod(freq*t,1));
% % example 1 : positive square wave / mono phasic pulse train
% square_wav = 0.5*(sign(sin(angl))+1);
% plot(t,square_wav);
% example 2 : bi phasic pulse train
interphase_gap = 10e-3; % gap (in second)
gap_angl = 2*pi*freq*interphase_gap;
angl(angl<=gap_angl/2) = 0;
angl(abs(2*pi-angl)<=gap_angl/2) = 0;
angl(abs(pi-angl)<=gap_angl/2) = 0;
biphasic_pulse_train = sign(sin(angl));
% plot(t,angl);
plot(t,biphasic_pulse_train);

19 Comments

thnx a lot ^^
could u please help me apply this pulse train on other signals to get finally pulses that take the shape each signal like this..
hello
sorry, I'm not sure to understand what you need ? you want to simulate the same type of signals like above ?
a mix of mon and bi phasic pulses ?
Abir Ouji
Abir Ouji on 20 Nov 2020
Edited: Abir Ouji on 20 Nov 2020
hi
I wanna mix an envelope of audio signal this :
and a biphasic pulses like this
to get this :
hello
so you have to create a bi phasic pulse train signal that has ame length as your audio signal
at th time index of when the pulse signal gets from positive to negative, you pick the amplitude of the audio signal and multiply the bi phasic pulse train signal with that amplitude info
i got it but how is that with matlab ? I m a beginner and still learning matlab. could u please help me ? :)
hello
ok, can yu share your audio file ?
second, can yu define when the pulses are defined on the time axis ?
hiii, i couldn't share the audio file but u can use any audio file.wav u want, coz i need to try everytime an audio with the commande [y,fs]=audioread('filename.wav');
second, the pulses should be like these ones:
with a rate=833 pulse/sec and d=33 msec/phase and it start's after 1/rate duration
hello I think I have a code that works but I have a problem with the values you gave above
if d is 33 msec (confirm) , so 33e-3 s , you cannot have 833 pulses per seconde because each pulse duration would be 66 msec, so max 15 pulses per second is doable, and it woul look like there is no time space between two consecutive pulses
so, this is the code, but the demo is made with rate = 1;
NB this is made with Fs = 11 kHz wav file
infile='DirectGuitar.wav';
% current sample is 11kHz so 0-3 ms is 0 - 33 samples
% read the sample waveform
[x,Fs] = audioread(infile);
samples = length(x);
time =(0:samples-1)/Fs;
% normalize x to +/- 1 amplitude
x = x ./ (max(abs(x)));
% square pulse wave
% rate=833 pulse/sec and d=33 msec/phase and it start's after 1/rate duration
rate = 1; % Fs/833;
start_time = 1; %rate/Fs; % in seconds
gap_seconds = 33e-3; % please double check
[BP_train, signal_pulsed] = bptrain(Fs,samples,rate,start_time,gap_seconds,x);
% plot
figure(1);
subplot(3,1,1),plot(time,x,'b');grid
title('input waveform');
subplot(3,1,2),plot(time,BP_train,'b');grid
title('pulse train');
subplot(3,1,3),plot(time,signal_pulsed,'b');grid
title('pulsed waveform');
function [biphasic_pulse_train, signal_pulsed] = bptrain(Fs,samples,rate,start_time,gap_seconds,signal)
start_sample = floor(start_time*Fs);
gap_angl = 2*pi*rate*gap_seconds;
sin_gap_angl = sin(gap_angl);
time =(0:samples-start_sample)/Fs;
angl = 2*pi*(mod(rate*time,1));
time = (0:samples-1)/Fs;
out = zeros(samples,1);
angl = [zeros(1,start_sample-1) angl];
ind_pos = find(angl>0 & angl<=gap_angl);
out(ind_pos) = 1;
ind_neg = find(angl>gap_angl & angl<=2*gap_angl);
out(ind_neg) = -1;
% create a enveloppe of the signal before applying the product with the
% pulse train
N = 1000;
envelop = myslidingavg(abs(signal), N);
biphasic_pulse_train = out;
signal_pulsed = biphasic_pulse_train.*envelop;
end
function out = myslidingavg(in, N)
% OUTPUT_ARRAY = MYSLIDINGAVG(INPUT_ARRAY, N)
%
% The function 'slidingavg' implements a one-dimensional filtering, applying a sliding window to a sequence. Such filtering replaces the center value in
% the window with the average value of all the points within the window. When the sliding window is exceeding the lower or upper boundaries of the input
% vector INPUT_ARRAY, the average is computed among the available points. Indicating with nx the length of the the input sequence, we note that for values
% of N larger or equal to 2*(nx - 1), each value of the output data array are identical and equal to mean(in).
%
% * The input argument INPUT_ARRAY is the numerical data array to be processed.
% * The input argument N is the number of neighboring data points to average over for each point of IN.
%
% * The output argument OUTPUT_ARRAY is the output data array.
if (isempty(in)) | (N<=0) % If the input array is empty or N is non-positive,
disp(sprintf('SlidingAvg: (Error) empty input data or N null.')); % an error is reported to the standard output and the
return; % execution of the routine is stopped.
end % if
if (N==1) % If the number of neighbouring points over which the sliding
out = in; % average will be performed is '1', then no average actually occur and
return; % OUTPUT_ARRAY will be the copy of INPUT_ARRAY and the execution of the routine
end % if % is stopped.
nx = length(in); % The length of the input data structure is acquired to later evaluate the 'mean' over the appropriate boundaries.
if (N>=(2*(nx-1))) % If the number of neighbouring points over which the sliding
out = mean(in)*ones(size(in)); % average will be performed is large enough, then the average actually covers all the points
return; % of INPUT_ARRAY, for each index of OUTPUT_ARRAY and some CPU time can be gained by such an approach.
end % if % The execution of the routine is stopped.
out = zeros(size(in)); % In all the other situations, the initialization of the output data structure is performed.
if rem(N,2)~=1 % When N is even, then we proceed in taking the half of it:
m = N/2; % m = N / 2.
else % Otherwise (N >= 3, N odd), N-1 is even ( N-1 >= 2) and we proceed taking the half of it:
m = (N-1)/2; % m = (N-1) / 2.
end % if
for i=1:nx, % For each element (i-th) contained in the input numerical array, a check must be performed:
dist2start = i-1; % index distance from current index to start index (1)
dist2end = nx-i; % index distance from current index to end index (nx)
if dist2start<m || dist2end<m % if we are close to start / end of data, reduce the mean calculation on centered data vector reduced to available samples
dd = min(dist2start,dist2end); % min of the two distance (start or end)
else
dd = m;
end % if
out(i) = mean(in(i-dd:i+dd)); % mean of centered data , reduced to available samples at both ends of the data vector
end % for i
hello,
it shows an error for me and says "Function definitions are not permitted in this context" :/
i got it ! i think this version of matlab (R2014b) does not count "bptrain" syntax :/
hello
maybe you should store the functions as separate files
Abir Ouji
Abir Ouji on 21 Nov 2020
Edited: Abir Ouji on 21 Nov 2020
I could really do that ! I thought that I couldnt use any other function apart from matlab R2014b functions. if I store it in a separate file how could i call it in my code ?
you can of course create or download new functions (from the File Exchange for example)
there are no limits to expand matlab functionnalities
for your code the main code for you would be only this part
infile='DirectGuitar.wav';
% current sample is 11kHz so 0-3 ms is 0 - 33 samples
% read the sample waveform
[x,Fs] = audioread(infile);
samples = length(x);
time =(0:samples-1)/Fs;
% normalize x to +/- 1 amplitude
x = x ./ (max(abs(x)));
% square pulse wave
% rate=833 pulse/sec and d=33 msec/phase and it start's after 1/rate duration
rate = 1; % Fs/833;
start_time = 1; %rate/Fs; % in seconds
gap_seconds = 33e-3; % please double check
[BP_train, signal_pulsed] = bptrain(Fs,samples,rate,start_time,gap_seconds,x);
% plot
figure(1);
subplot(3,1,1),plot(time,x,'b');grid
title('input waveform');
subplot(3,1,2),plot(time,BP_train,'b');grid
title('pulse train');
subplot(3,1,3),plot(time,signal_pulsed,'b');grid
title('pulsed waveform');
the rest , that is the 2 subfunctions are stores are separate m functions , either in the same directory as the main code or as your own toolbox (create a folder and add it to the matlab path)
hello again
in case you need more info on functions , see the help
https://fr.mathworks.com/help/matlab/ref/function.html
Abir Ouji
Abir Ouji on 22 Nov 2020
Edited: Abir Ouji on 22 Nov 2020
hello
I did store two separate m functions. first i doesn t work coz the variables in in the function instruction are not define. for example for the function myslidingavg i needed to define the two variable N and in ! so I did and it works.
but could u please tell me what are the signification of every variable of the function bptrain and its role? so i could solve the rate and duration problem.
I only have less than a week to finish this but still doesn t work and I started to get nervous and stressed :(
hello
I have cleaned my code and aded more comments everywhere to help you
also there is the main code and separate subfunctions
hope it helps
hiii :)
it does wooooooooooork :D
thank you a lot Mathieu NOE you are so kind, helpful and gentle. thank you :)
Glad it helps you
now you can relax a bit !!
good luck for the future
yes I feel relieved now hh
thank you :)
you too

Sign in to comment.

More Answers (0)

Asked:

on 9 Nov 2020

Commented:

on 22 Nov 2020

Community Treasure Hunt

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

Start Hunting!