how to cut audio file into small files using silent

15 views (last 30 days)
hi all
i have an audio file like this
i want to cut it into 5 small files and save them separated to work on. any help
i am using this code but it remove silent only. I don't know how to cut it.
% Read in data and plot it.
[y, Fs] = readwav('F1.wav');
figure, plot(y, 'b-');
%Find the envelope by taking a moving max operation, imdilate.
envelope = imdilate(abs(y), true(1501, 1));
% Plot it.
hold on;
plot(envelope, 'r-', 'LineWidth', 2);
plot(-envelope, 'r-', 'LineWidth', 2);
% Find the quiet parts.
quietParts = envelope < 0.07; % Or whatever value you want.
% Cut out quiet parts and plot.
yEdited = y; % Initialize
yEdited(quietParts) = [];
figure,plot(yEdited, 'b-', 'LineWidth', 2);

Answers (1)

Walter Roberson
Walter Roberson on 12 Nov 2016
You want to look for transitions from quietParts being true to it being false in order to locate the beginning of non-quiet, and you want to look for transitions from quietParts being false to it being true in order to locate the ending of non-quiet.
A trick involved is to use
quiet_row = quietParts(:).'; %need it as row vector
A = strfind(quiet_row, [1 0]); %silence ends
B = strfind(quiet_row, [0 1]); %silence starts
You need to work out the boundary conditions to figure out whether to use those indices exactly as-is or one before or one after.
Also, you need to be concerned about whether the file begins in silence or not, and whether it ends in silence or not. For that purpose it can be useful to pad quiet_row with false or true on both sides (I leave it to you to work out which is appropriate.)
  7 Comments
Joenam Coutinho
Joenam Coutinho on 18 Feb 2022
Hello Walter,
I tried to implement this, And I understood the concept. But I am finding it difficult to plot the segments I want.
How would you plot the waveforms that dont include the silent segments.
Walter Roberson
Walter Roberson on 18 Feb 2022
Edited: Walter Roberson on 18 Feb 2022
sound_row = ~quietParts(:).'; %need it as row vector
starts = strfind([false sound_row], [0 1]); %sound starts
stops = strfind([sound_row false], [1 0]); %sound end
times = (0:length(sound_row)-1)/Fs;
for K = 1 : length(starts)
extracted_time = times(starts(K):stops(K));
extracted_signal = y(starts(K):stops(K),:);
plot(extracted_time, extracted_signal);
%or write the extacted signal to a file if that is what you want
end
Or, since it is for plotting purposes:
yc = y;
yc(quiet_parts,:) = nan;
times = (0:length(quiet_parts)-1)/Fs;
plot(times, yc);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!