Clear Filters
Clear Filters

Converting a set of audio signals to images and saving them.

15 views (last 30 days)
Hello friends. I want to do spectral conversion on my audio signals and then save the output images somewhere else. But I don't know how to read and write in the images.
% make a list of wave files in training_data folder
cd training_data_out\
folderInfo = dir('**/*.wav');
folderInfo1 = dir('**/*.tsv');
folderInfo2 = dir('**/*.txt');
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
for i=1:length(folderInfo)
x = audioread(folderInfo(i).name);
% signal filtering
xf = filter(b,1,x);
[st,t,f]=st(xf,25,350,1,1);
m=abs (st);
imagesc(m (:,:,i))
pause(.5)
end
  1 Comment
Jan
Jan on 6 Feb 2023
"But I don't know how to read and write in the images." - use imread and imwrite.
Avoid to add a folder to Matlab's path only to read or write some files. This is a source of unexpected behavior, if the folders contain m-files. Use full paths instead and do not change the current folder:
folder = cd; % Or prefer an absolute path here also
folderInfo = dir(fullfile(folder, 'training_data_out', '**/*.wav');
Remember that cd() commands can be called unexpectedly in callbacks of timers or GUIs during your code runs. This is a typical source of bugs and can be avoided easily.

Sign in to comment.

Answers (1)

DGM
DGM on 6 Feb 2023
I don't know what st() is, but I have to assume it's a function -- possibly an STFT tool? If so, then it won't be a function anymore. You've just created a variable with the same name.
[st,t,f]=st(xf,25,350,1,1);
If that's a correct assumption, then I'd expect st to be a 2D array. I would have no reason to expect that each file would produce a 3D array with as many pages as there are audio files in the directory. So I'd have to conclude that this isn't the way to address m.
imagesc(m(:,:,i))
To generate a pseudocolored image from real-valued data on an arbitrary scale:
% a page
st = 10*randn(100,100); % random fake data (2D)
st = abs(st);
% convert to an RGB image using uniform quantization
N = 256; % specify quantization levels
CT = jet(N); % specify colormap to use
outpict = ind2rgb(gray2ind(mat2gray(st),N),CT);
% do something with it
imshow(outpict)
imwrite(outpict,'myimage.png')
Just a guess, but this may be relevant. Sometimes similar questions pop up at the same time when multiple people are working on the same assignment.

Community Treasure Hunt

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

Start Hunting!