Cut Audio Signals (.wav) in specific time.Create a repeat loop to reach the desired time

6 views (last 30 days)
Hello friends. I want to make all the audio signals of a database the same in terms of time (for example 12.5 seconds). What loop and functions should be used?
Because some small signals must be repeated to reach 12.5 seconds.Some data cut and some are exactly the same size.So, apparently, we will be faced with 3 states, bigger, smaller and equal
% make a list of wave files in training_data folder
cd training_data\
folderInfo = dir('**/*.wav');
folderInfo1 = dir('**/*.tsv');
folderInfo2 = dir('**/*.txt');
cd ..\
addpath training_data\
% fir bandpass filter design with fc1 = 20/fs/2 and fc2 = 500/fs/2 with
% fs=4000
f = [0 0.005 0.015 0.245 0.255 1];
a = [0 0 1 1 0 0];
b = firpm(100,f,a);
% feature vector calculation
for i=1:length(folderInfo)
x = audioread(folderInfo(i).name);
% signal filtering
xf = filter(b,1,x);

Accepted Answer

Mathieu NOE
Mathieu NOE on 1 Feb 2023
Edited: Mathieu NOE on 1 Feb 2023
hello
try this
I tested the code for mono and stereo wav files - should work even for more channels data
now please notice that I made sure to use the correct sampling rate associated with each file - in case they would differ from one file to another, the code will ensure that the total duration is correct - even though your filter would not have the same effect unless you include its coefficients computation inside the main loop (and take account of Fs);
folderInfo = dir('**/*.wav');
folderInfo1 = dir('**/*.tsv');
folderInfo2 = dir('**/*.txt');
cd ..\
addpath training_data\
% fir bandpass filter design with fc1 = 20/fs/2 and fc2 = 500/fs/2 with
% fs=4000
f = [0 0.005 0.015 0.245 0.255 1];
a = [0 0 1 1 0 0];
b = firpm(100,f,a);
% feature vector calculation
duration_target = 12.5; % seconds
for i=1:length(folderInfo)
filename = folderInfo(i).name
[x,Fs] = audioread(filename);
% signal filtering
xf = filter(b,1,x);
% main code
dt = 1/Fs;
[samples,channels] = size(x);
duration = (samples-1)*dt;
duration_ratio = duration/duration_target;
target_duration_samples = (1+duration_target*Fs);
if duration_ratio>1 % truncate
xf_out = xf(1:target_duration_samples,:);
else % do nothing or expand(by repetition and trimming
repetitions = ceil(1/duration_ratio);
xf_out = repmat(xf,repetitions,1); % duplicate (by excess)
xf_out = xf_out(1:target_duration_samples,:); % exact number of samples
end
% export to wav
filename_out = [filename(1:length(filename)-4) '_out.wav']
audiowrite(filename_out,xf_out,Fs);
end
  8 Comments
omid
omid on 11 Feb 2023
Thank you, dear friend. With a small change (removing the current folder), I run the code assuming st = rand(100,100), which works correctly. But why do I get this message when I run the main conversion?
"Insufficient number of outputs from right hand side of equal sign to satisfy assignment."
clc
clear
close all
cd training_data_out\
folderInfo = dir('**/*.wav');
cd ..\
addpath training_data_out\
% fir bandpass filter design with fc1 = 20/fs/2 and fc2 = 500/fs/2 with
% fs=4000
f = [0 0.005 0.015 0.245 0.255 1];
a = [0 0 1 1 0 0];
b = firpm(100,f,a);
% feature vector calculation
duration_target = 12.5; % seconds
% working (current) folder
for i=1:length(folderInfo)
filename = folderInfo(i).name
[x,Fs] = audioread(filename);
% signal filtering
xf = filter(b,1,x);
% main code
dt = 1/Fs;
[samples,channels] = size(x);
duration = (samples-1)*dt;
duration_ratio = duration/duration_target;
target_duration_samples = (1+duration_target*Fs);
if duration_ratio>1 % truncate
xf_out = xf(1:target_duration_samples,:);
% debug
[samples,channels] = size(xf_out);
duration = (samples-1)*dt
else % do nothing or expand(by repetition and trimming
repetitions = ceil(1/duration_ratio);
xf_out = repmat(xf,repetitions,1); % duplicate (by excess)
xf_out = xf_out(1:target_duration_samples,:); % exact number of samples
% debug
[samples,channels] = size(xf_out);
duration = (samples-1)*dt
end
% % st transform
[st,t,f]=st(xf_out,25,350,1,1); % still need to bo worked out
st=abs(st);
% st = rand(100,100); % just for the sake of the demo until st is fixed (above)
figure(i),
imshow(st)
filename_out = [filename(1:length(filename)-4) 'o.png']
imwrite(st,filename_out);
end
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in OMG (line 52)
[st,t,f]=st(xf_out,25,350,1,1); % still need to bo worked out

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 11 Feb 2023
[st,t,f]=st(xf_out,25,350,1,1); % still need to bo worked out
The first time you execute that statement, there is not variable named st so MATLAB looks for an st function. The function is run, and you assign the outputs to several variables, including st
The second time you execute that statement, there is now a variable named st so that is an indexing request. But indexing can never have multiple outputs, so MATLAB complains.
  9 Comments

Sign in to comment.


omid
omid on 20 Feb 2023
Hello friends. I executed this code and saved it with .mat extension to save time.Due to the large number of samples, it took 4 hours to perform the calculations, and the size of the saved file is 360 MB, which seems reasonable (that is, the signal-to-image conversions are apparently done correctly).
clc
clear
close all
cd training_data_out\
folderInfo = dir('**/*.wav');
cd ..\
addpath training_data_out\
% working (current) folder
for i=1:length(folderInfo)
filename = folderInfo(i).name
[x,Fs] = audioread(filename);
% % st transform
[st_out,t,f]=st(x,25,350,1,1); % still need to bo worked out
zz=abs(st_out);
end
1= Now I can't have the output of the photos(how should I write this new ring?)
2- As it is clear in the uploaded photo (a1), the number of rows and columns of the photo is 326*5001, but with the command print('-dpng','-r300',filename_out) the rows and columns are messed up.
3=Look at the next photo (a2). How can I prevent these numbers from being printed in the output?
  3 Comments
omid
omid on 22 Feb 2023
Excuse me, dear Walter, can you put what you said (items 2 and 3) into my code.Because I didn't get an answer, I must have misunderstood.
Walter Roberson
Walter Roberson on 22 Feb 2023
Edited: Walter Roberson on 22 Feb 2023
projectdir = 'training_data_out';
imgext = '.png';
folderInfo = dir( fullfile(projectdir, '**', '*.wav');
cmap = parula(256);
% working (current) folder
for i=1:length(folderInfo)
filename = fullfile(folderInfo(i).folder, folderInfo(i).name);
[folder, basename] = fileparts(filename);
outfilename = fullfile(folder, [basename imgext]);
[x,Fs] = audioread(filename);
% % st transform
[st_out,t,f]=st(x,25,350,1,1); % still need to bo worked out
zz=abs(st_out);
zzind = uint8(rescale(zz, 0, 255));
imwrite(zzind, cmap, outfilename);
end
This creates .png with the same file name in the same folder as the original wav file.
Note that there is no colorbar and no labels or title. Also note that the data is scaled relative to itself, so the colors have no absolute meaning -- if one zz result ranged up to +738 then that +738 would be mapped to the last colour in cmap, and if a different zz result ranged up to +1192 then that +1192 would be mapped to the last colour in cmap for that image. This is the same behaviour you would get with imagesc()

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!