Cut Audio Signals (.wav) in specific time.Create a repeat loop to reach the desired time
4 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
Mathieu NOE
2023-2-1
编辑:Mathieu NOE
2023-2-1
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 个评论
更多回答(2 个)
Walter Roberson
2023-2-11
[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 个评论
Walter Roberson
2023-2-13
change
duration = (samples-1)*dt
to add a semi-colon
duration = (samples-1)*dt;
omid
2023-2-20
3 个评论
Walter Roberson
2023-2-22
编辑:Walter Roberson
2023-2-22
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()
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!