How could I generate a biphasic pulse train?
4 次查看(过去 30 天)
显示 更早的评论
Abir Ouji
2020-11-9
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 ?
采纳的回答
Mathieu NOE
2020-11-9
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 个评论
Abir Ouji
2020-11-10
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..

Mathieu NOE
2020-11-12
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 ?
Mathieu NOE
2020-11-20
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
Abir Ouji
2020-11-20
i got it but how is that with matlab ? I m a beginner and still learning matlab. could u please help me ? :)
Mathieu NOE
2020-11-20
hello
ok, can yu share your audio file ?
second, can yu define when the pulses are defined on the time axis ?
Abir Ouji
2020-11-20
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
Mathieu NOE
2020-11-20
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
Abir Ouji
2020-11-20
hello,
it shows an error for me and says "Function definitions are not permitted in this context" :/
Abir Ouji
2020-11-20
i got it ! i think this version of matlab (R2014b) does not count "bptrain" syntax :/
Mathieu NOE
2020-11-21
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)
Mathieu NOE
2020-11-22
hello again
in case you need more info on functions , see the help
https://fr.mathworks.com/help/matlab/ref/function.html
Abir Ouji
2020-11-22
编辑:Abir Ouji
2020-11-22
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 :(
Mathieu NOE
2020-11-22
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
Abir Ouji
2020-11-22
hiii :)
it does wooooooooooork :D
thank you a lot Mathieu NOE you are so kind, helpful and gentle. thank you :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 AI for Audio 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)