question about generate a toneburst ( sound) in every ear

조회 수: 8 (최근 30일)
mohadeseh zamani
mohadeseh zamani 2021년 8월 12일
댓글: mohadeseh zamani 2021년 9월 2일

Hello everybody, I am trying to generate a toneburst in either left and right ear for example if f=250 Hz and duration of time that sound is produced in the ear is 1 seconds and the distance between the two sounds is 4 second ( first sound in the left ear and the right sound in the right ear) my problem is that generating sound in left and right ear if you know it please help me I really neeed this part.

채택된 답변

Star Strider
Star Strider 2021년 8월 12일
Try this:
Fs = 44100; % Sampling Frequency
Fc = 250; % Tone Frequency
durn = 1; % Duration (Sec)
t = linspace(0, durn*Fs-1, durn*Fs)/Fs; % Time VEctor
s = sin(2*pi*Fc*t); % Generate Signal
Fn = Fs/2;
L = numel(t);
N = 2^nextpow2(L);
FTs = fft(s,N)/L;
Fv = linspace(0, 1, N/2+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTs(Iv))*2) % Check Frequency
grid
xlim([0 500])
for k = 1:2
sm = zeros(L,2);
sm(:,k) = s;
sm(1:5,:)
sound(sm,Fs) % Left Ear First, Right Ear Second
pause(5) % 5-Second Pause
end
Experiment to get different results.
.
  댓글 수: 1
mohadeseh zamani
mohadeseh zamani 2021년 9월 2일
I really appreciate for your helping, I did not think anybody help here and exactly right now I check my matlab accaunt and saw your answer, so before that I check your answer I wrote a simple code myself that maybe was true , right now I have a question in another part of my code (line 67 in my code), inside of the memory I have 45 sound that I generate and right now I want to repeat every sound 10 times (45*10=450 sound) and after that ​I want to present 450 sounds to the left and right ears randomly.
Hear is my code. can you help me??? (if I did not explain very well please tell me to explain again)
clear
clc
close all
tic;
SamplingRate_Hz=48000;
F=[ 250 500 1000 2000 4000 ];
A=[5 10 15 ];
T=[1 2 3 ];
memoryR{1,1}{1,1}{1,1}={0};
memoryL{1,1}{1,1}{1,1}={0};
memory{1,1}{1,1}{1,1}={0};
for i=1:length(F)
for j=1:length(T)
for k=1:length(A)
Frequency_Hz = F(1,i);
time_p = T(1,j);
Amplitude = A(1,k);
t=0:(1/SamplingRate_Hz):(time_p-1/Frequency_Hz);
y=Amplitude*sin(2*pi*Frequency_Hz*t);
xt_ramped = toneburst(SamplingRate_Hz,y); %% toneburst is my function
Out = [zeros(size(t)); xt_ramped]'; % Right
plot(Out);
OutR = [zeros(size(t)); xt_ramped]'; % Right
OutL = [ xt_ramped ;zeros(size(t))]'; % Left
%% Right
memoryR{1,i}{1,j}{1,k} = OutR;
save AuditorySignalRight.mat OutR
% Left
memoryL{1,i}{1,j}{1,k} = OutL;
save AuditorySignalLeft.mat OutL
%%
% % Clear them out of the workspace
clear OutR
clear OutL
% %
% % Load them again
load AuditorySignalRight.mat
load AuditorySignalLeft.mat
filenameRight = 'AuditorySignalRight.wav';
filenameLeft = 'AuditorySignalLeft.wav';
audiowrite(filenameRight,OutR,SamplingRate_Hz);
audiowrite(filenameLeft,OutL,SamplingRate_Hz);
clear OutR SamplingRate_Hz
clear OutL SamplingRate_Hz
% % Read the data back into MATLAB using audioread.
[OutR,SamplingRate_Hz] = audioread(filenameRight);
[OutL,SamplingRate_Hz] = audioread(filenameLeft);
% % Listen to the audio.
sound(OutR,SamplingRate_Hz); %% sound in right , you shoud put a break point
sound(OutL,SamplingRate_Hz); %% sound in left
%
%
% plot(OutR)
% plot(OutL)
end
end
end
%for i=1:450
memory={memoryL memoryR}; %% I have problem this part, I want to repeat 10 times of each sound (45 sound) that I made (Total sound 45*10=450) and after that present all of them randomly to left and right ear????
R1 = memory(randperm(2,1));
R2=R1{1,1}(randperm(1,1));
R3=R2{1,1}(randperm(1,1));
R4=R3{1,1}(randperm(1,1));
R5= cell2mat(R4);
save AuditorySignal.mat R5
% % Clear them out of the workspace
clear R5
% % Load them again
load AuditorySignal.mat
filename = 'AuditorySignal.wav';
audiowrite(filename,R5,SamplingRate_Hz);
clear R5 SamplingRate_Hz
% % Read the data back into MATLAB using audioread.
[R5,SamplingRate_Hz] = audioread(filename);
% % Listen to the audio.
sound(R5,SamplingRate_Hz);
%end
%%%%%
here is my function (toneburst)
function xt_ramped = toneburst(SamplingRate_Hz,y)
xt =y;
fs=SamplingRate_Hz;
ramp_dur=0.0025; %ramp duration in seconds
%%
%setup ramp
rampSamps = floor(fs*ramp_dur);
window=hanning(2*rampSamps)'; %hanning window is cosine^2 this will change depending on the kind of ramp you want
w1=window(1:ceil((length(window))/2)); %use the first half of hanning function for onramp
w2=window(ceil((length(window))/2)+1:end); %use second half of hanning function of off ramp
w_on_xt = [w1 ones(1,length(xt)-length(w1))];
w_off_xt = [ones(1,length(xt)-length(w2)) w2];
xt_ramped = xt.*w_on_xt.*w_off_xt;
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dave B
Dave B 2021년 8월 12일
The sound function accepts a two column matrix for y, the first column corresponds to the left channel, and the second column corresponds to the right channel.
Here's a demo with a 1 second 400Hz sound on the left followed by a 1 second 600Hz sound on the right:
t=linspace(0,1,44100);
f=400;
yleft=sin(t*2*pi*f);
f=600;
yright=sin(t*2*pi*f);
y=zeros(44100*2,2);
y(1:length(yleft),1)=yleft;
y(length(yleft)+(1:length(yright)),2)=yright;
% For visualization
stackedplot(y,"DisplayLabels",["Left" "Right"])
sound(y,44100)
  댓글 수: 1
mohadeseh zamani
mohadeseh zamani 2021년 9월 2일
I really appreciate for your helping, I did not think anybody help here and exactly right now I check my matlab accaunt and saw your answer, so before that I check your answer I wrote a simple code myself that maybe was true , right now I have a question in another part of my code (line 67 in my code), inside of the memory I have 45 sound that I generate and right now I want to repeat every sound 10 times (45*10=450 sound) and after that ​I want to present 450 sounds to the left and right ears randomly.
Hear is my code. can you help me??? (if I did not explain very well please tell me to explain again)
clear
clc
close all
tic;
SamplingRate_Hz=48000;
F=[ 250 500 1000 2000 4000 ];
A=[5 10 15 ];
T=[1 2 3 ];
memoryR{1,1}{1,1}{1,1}={0};
memoryL{1,1}{1,1}{1,1}={0};
memory{1,1}{1,1}{1,1}={0};
for i=1:length(F)
for j=1:length(T)
for k=1:length(A)
Frequency_Hz = F(1,i);
time_p = T(1,j);
Amplitude = A(1,k);
t=0:(1/SamplingRate_Hz):(time_p-1/Frequency_Hz);
y=Amplitude*sin(2*pi*Frequency_Hz*t);
xt_ramped = toneburst(SamplingRate_Hz,y); %% toneburst is my function
Out = [zeros(size(t)); xt_ramped]'; % Right
plot(Out);
OutR = [zeros(size(t)); xt_ramped]'; % Right
OutL = [ xt_ramped ;zeros(size(t))]'; % Left
%% Right
memoryR{1,i}{1,j}{1,k} = OutR;
save AuditorySignalRight.mat OutR
% Left
memoryL{1,i}{1,j}{1,k} = OutL;
save AuditorySignalLeft.mat OutL
%%
% % Clear them out of the workspace
clear OutR
clear OutL
% %
% % Load them again
load AuditorySignalRight.mat
load AuditorySignalLeft.mat
filenameRight = 'AuditorySignalRight.wav';
filenameLeft = 'AuditorySignalLeft.wav';
audiowrite(filenameRight,OutR,SamplingRate_Hz);
audiowrite(filenameLeft,OutL,SamplingRate_Hz);
clear OutR SamplingRate_Hz
clear OutL SamplingRate_Hz
% % Read the data back into MATLAB using audioread.
[OutR,SamplingRate_Hz] = audioread(filenameRight);
[OutL,SamplingRate_Hz] = audioread(filenameLeft);
% % Listen to the audio.
sound(OutR,SamplingRate_Hz); %% sound in right , you shoud put a break point
sound(OutL,SamplingRate_Hz); %% sound in left
%
%
% plot(OutR)
% plot(OutL)
end
end
end
%for i=1:450
memory={memoryL memoryR}; %% I have problem this part, I want to repeat 10 times of each sound (45 sound) that I made (Total sound 45*10=450) and after that present all of them randomly to left and right ear????
R1 = memory(randperm(2,1));
R2=R1{1,1}(randperm(1,1));
R3=R2{1,1}(randperm(1,1));
R4=R3{1,1}(randperm(1,1));
R5= cell2mat(R4);
save AuditorySignal.mat R5
% % Clear them out of the workspace
clear R5
% % Load them again
load AuditorySignal.mat
filename = 'AuditorySignal.wav';
audiowrite(filename,R5,SamplingRate_Hz);
clear R5 SamplingRate_Hz
% % Read the data back into MATLAB using audioread.
[R5,SamplingRate_Hz] = audioread(filename);
% % Listen to the audio.
sound(R5,SamplingRate_Hz);
%end
%%%%%
here is my function (toneburst)
function xt_ramped = toneburst(SamplingRate_Hz,y)
xt =y;
fs=SamplingRate_Hz;
ramp_dur=0.0025; %ramp duration in seconds
%%
%setup ramp
rampSamps = floor(fs*ramp_dur);
window=hanning(2*rampSamps)'; %hanning window is cosine^2 this will change depending on the kind of ramp you want
w1=window(1:ceil((length(window))/2)); %use the first half of hanning function for onramp
w2=window(ceil((length(window))/2)+1:end); %use second half of hanning function of off ramp
w_on_xt = [w1 ones(1,length(xt)-length(w1))];
w_off_xt = [ones(1,length(xt)-length(w2)) w2];
xt_ramped = xt.*w_on_xt.*w_off_xt;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by