How do I select segments of a matrix based on values in a CSV within a loop
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to read in a CSV with integer values and want to select portions of a wav file from which are of interest (a specific time period represented by valuess in y) within a loop. What can I do to make this happen? Example code below:
M = csvread(segments.csv) % 2 columns, 280 rows
for i = 31:3527
[y,Fs] = audioread(strcat(DirIn,'/',filelist(i).name)); % read in wav file
z = y([Col 1, Row i+31:Col 2, Row i+31], [1]) % Need help here
snips_db = 20*log10(z) - Sensitivity - Gain
c(end+1;) = snips_db
end
댓글 수: 4
Star Strider
2022년 5월 9일
An extended description of the problem:
I have a 250 24min wav files. For each file I have a random 30sec period which I want to pull out for analysis. The start times and end times for each 30 sec period is in a CSV.
I want to create a loop that reads in the audiofile, extracts the 30 sec period from the file, analyzes and repears. E.g., Read in file one, downselect file one wav to samples in row one of the CSV. Read in file two, downselct file two to samples in row two of the csv. Etc.
.
Jan
2022년 5월 10일
If the command to crop the signal does not include a reference to M, insert a reference to M. The readers cannot know the contents of M, so it is your turn either to implement it or to explain the deatails. If we see a not working code, we cannot guess magically its intention:
z = y([Col 1, Row i+31:Col 2, Row i+31], [1]) % Need help here
Which kind of help do you need? What is "Row" and "Col"? Why "+31"? I assume Fs helps to get the indices for specific times.
답변 (1개)
Aditya
2023년 11월 22일
Hi Benjamin,
I understand that you want to read segments from a CSV file and use those segments to extract specific time periods from a series of WAV files.
Assuming that your CSV file contains start and end times in seconds for each segment, and you want to extract these segments from the WAV files, here's how you can modify your code:
for i = 31:3527
[y, Fs] = audioread(fullfile(DirIn, filelist(i).name)); % read in wav file
% Extract segments based on the CSV values
for j = 1:size(M, 1) % Iterate over each row in the CSV
startTime = M(j, 1); % Start time in seconds
endTime = M(j, 2); % End time in seconds
% Convert times to sample indices
startSample = round(startTime * Fs) + 1; % Add 1 because MATLAB indices start at 1
endSample = round(endTime * Fs);
% Extract the segment
if startSample > 0 && endSample <= length(y)
z = y(startSample:endSample, 1); % Assuming you want the first channel
snips_db = 20 * log10(abs(z)) - Sensitivity - Gain;
c(end+1;) = snips_db;
end
end
end
I've replaced “strcat()” with “fullfile()” for better path handling. Additionally, I've introduced a nested for loop that iterates through the rows of the CSV file to read the start and end times, converting them into sample indices for the WAV file data extraction. This code is a template, and you may adjust it as needed to suit your specific requirements.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!