how can extract data from txt file?

10 次查看(过去 30 天)
GreyHunter
GreyHunter 2020-10-6
编辑: TED MOSBY 2024-11-18,19:55
I am trying to read a specific name from the text file which correspond to mseconds( for example Jim and John occurances) and depends of the timing correspond to the name I would like to extract an audio signal but only the specified mseconds which are read from the txt file.
my current code:
filename= 'test.txt';
[y, Fs]= audioread(filename);
cutData= y(((Fs*(652-1))+1:Fs*(767-1)/2-10,:);
plot(linspace(0.652,0.767,length(cutData)),cutData);

回答(1 个)

TED MOSBY
TED MOSBY 2024-11-13,4:37
编辑:TED MOSBY 2024-11-18,19:55
To extract specific audio segments based on the timing information in your text file, you'll need to read the text file, parse the timing and name information, and then extract the corresponding segments from your audio file.The steps are implemented in the code below:
% Load the audio file
filename = 'your_audio_file.wav'; % Replace with your actual audio file
[y, Fs] = audioread(filename);
% Open and read the text file
fileID = fopen('test.txt', 'r');
data = textscan(fileID, '%f %f %s');
fclose(fileID);
% Extract columns from the text data
startTimes = data{1};
endTimes = data{2};
names = data{3};
% Specify the name to extract (e.g., 'john' or 'jim')
targetName = 'jim'; % Change to 'john' if needed
% Loop through each entry in the text file
for i = 1:length(names)
if strcmp(names{i}, targetName)
% Convert milliseconds to sample indices
startSample = round((startTimes(i) / 1000) * Fs) + 1;
endSample = round((endTimes(i) / 1000) * Fs);
% Extract the audio segment
cutData = y(startSample:endSample, :);
end
end
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by