How to truncate number of sample in audiofiles using audioDatastore function in MATLAB?

Hi all
I am using audiodatadatastore function to classify my speech signal using wavelet scattering . Unfortunately all my audio files are having different length or sample size. I want to make it uniform for all files.
I did try following
% while hasdata(ads)
% [data,info] = read(ads);
%
% if length(data)<45000
% data=[data;zeros(45000-length(data),1)];
% else
% data=data(1:45000);
% end
% end
But that’s of no use. Help is highly appreciated.

6 Comments

kc, can you provide a little more info? Why is your code of no use? Is you goal to set all your signal lengths to 45000?
@jibrahim thank you sir for this promt reply.
I have two problems sir First one:
Actually I am working on a audio classification problem. I have almost 500 audio utterances. Here I follow https://www.mathworks.com/help/signal/examples/music-genre-classification-using-wavelet-scattering.html. But unfortunately my audio files are not of equal lengths. so this function: helperscatfeatures is not working for me. I tried extracting data length from audioDatastore but may be thats not defined. I am thinking is there any way to exctarct datalength(no of sample in audio files) using this audioDatastore.
and second issue is:
I have an intuition this scatter transform wont work well for my task, so I want to apply CNN on my raw data, but for that too 1D CNN is needed this layer is not defined in MATLAB deepNetworkDesigner app. I am using matlab 2018b. If you suggest i can open another question for this second problem.
Thanks in adavance
Yes sir I am using that example only there each speech sample is of 30 sec duration.

Sign in to comment.

 Accepted Answer

data(45001, :) = 0;
data = data(1:45000, 1);
No if needed, and handles multiple channels converting to single channel.

12 Comments

As of now in audioDatastor this term data is undefined
while hasdata(ads)
data = read(ads);
data(45001) = 0;
data = data(1:45000) ;
do something with data
end
This data samples can be reduced by using syntax A=audioread(adsTrain.Files{i}) ;
This in a loop as u explained ,but now the problem is when i execute it as per your direction its not modifying ads.Like when m asking for A=audioread(adsTrain.Files{10});its still showing data length 19000 not 45000. So ads.Train and ads.Test samples lengths remained same how to fix that.
Thank you :)
Also i m not getting accept answer link on ur last code solution plz help in that.
The code is not intended to modify ads. The loop is intended to be a cleaned-up version of the code you posted.
If you really wanted, you could have the loop write out modified audio files to another location, so that then you could point the regular audio data store to the location that only had files of the same length.
I would suggest, though, that you simply add a custom read function to the data store, with the custom read function using audioreader() and doing the truncation (or padding)
How can i edit inbuilt functions such as audioDatastore? I am sorry i think m taking you wrong sir, plz eloborate.
Although a tried using another function audioreader() and used it in my code afterwords , but its not making any changes in adsTrain etc. It still saying 'data' is undefined.
My soul purpose is to make my audio files of equal length of say 3 sec. Then probably I can use this mentioned example properly. Plz help if there is any manual way of doing so.
Regards
Look at this example of using the audioDataStore transform method to clip each sample to 5 seconds.
The idea is that you would create your original audio data store, and then call transform() on the audio data store, passing in the function that does the truncation. The result is a new audio data store, that you then pass on to your processing.
Thank you for this sir, But now its again stuck up with the error below
Undefined function 'transform' for input arguments of type 'audioDatastore'.
Oh is it not working due to this old version? Is there any solution for this.
It will take me more searching and testing. It turns out that audioDataStore ignores custom readFcn so it might be necessary to recode it for to use a datastore of type 'File'.
Sir is it possible for you to customise it now. That is the only issue I am facing right now... unequal sample lengths

Sign in to comment.

More Answers (0)

Asked:

kc
on 22 Apr 2020

Commented:

kc
on 25 Apr 2020

Community Treasure Hunt

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

Start Hunting!