Convert mat files to wav

80 views (last 30 days)
Robert Byrnes
Robert Byrnes on 9 Oct 2016
Commented: Stephen23 on 1 Jun 2018
I have followed the steps here as closely as I can: https://www.mathworks.com/help/matlab/ref/audiowrite.html
I am using MATLAB R2016b.
I am attempting to listen to these bird songs and save them as .wav files: http://datadryad.org/resource/doi:10.5061/dryad.9nv3b.
Audio_Bird9 is the smallest file, so am I working with this one specifically.
I have defined y = 1.0, and Fs = 48000, but I have not successfully created a .wav output file that produces sound.
I am sure they are lovely songs, and I would love to hear them, if anyone can offer me a working code. Thank you!
Robert

Accepted Answer

Star Strider
Star Strider on 9 Oct 2016
Edited: Star Strider on 9 Oct 2016
In Section 5 ‘Materials and Methods’, the authors note: ‘... pied butcherbird singing performances were digitally recorded at a sample rate of 44.1 kHz and 16 bits ...’
With that added bit of information, you can hear them in their full glory, for example:
d = load('Audio_Bird1.mat');
Bird1 = d.Audio_Bird1;
Fs = 44.1E+3; % Sampling Frequency
nBits = 16; % Bit Resolution
Audio_Bird1 = audioplayer(Bird1, Fs, nBits); % Create ‘audioplayer’ Object
audiowrite('Audio_Bird1.wav', Bird1, Fs, 'BitsPerSample',nBits, 'Comment','Australian pied butcherbird song - Bird1');
play(Audio_Bird1) % Play Song
EDIT Added sudiowrite call to save as .wav file.
  2 Comments
Amit Singh
Amit Singh on 1 Jun 2018
Edited: Amit Singh on 1 Jun 2018
What does second line of your code do?
Bird1 = d.Audio_Bird1;

Sign in to comment.

More Answers (3)

Massimo Zanetti
Massimo Zanetti on 9 Oct 2016
Edited: Massimo Zanetti on 9 Oct 2016
Here it is for listening at the audio in MAT format simply (Matlab uses default sample rate 8192 hertz, but true one is 44100, supply it as second parameter of the sound function):
load Audio_Bird9;
sound(Audio_Bird9,44100,16);
More flexible is
song=audioplayer(Audio_Bird9,44100,16);
play(song);
I tried listening to the file on my machine, it works (little noisy audio).
As Strider noticed true sample rate is 44100 at 16 bits. To save the file:
audiowrite('SongName.wav',Audio_Bird9,44100)

Image Analyst
Image Analyst on 9 Oct 2016
Try the attached m-file. It lets you pick a .mat file from a folder, then it saves the waveform as a .wav file, plots the waveform, and plays the sound as many times as you specify. Don't afraid that it's long. It's just because I made it fancy and flexible. Just copy, paste, and run it.
% Program to create a wave file from a .mat file with bird sounds in it.
% Bounds in .mat files can be found at http://datadryad.org/resource/doi:10.5061/dryad.9nv3b
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd;
% startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.mat');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
inputFullFileName = fullfile(folder, baseFileName)
fprintf('Full File Name = %s\n', inputFullFileName);
s = load('Audio_Bird9')
% The field is named after the file name, so we need to get the filename without the extension.
[~, fieldName, ~] = fileparts(baseFileName);
% Extract the amplitude data using a dynamic field name.
y = s.(fieldName);
% Set up the time axis:
t = 1:size(y, 1);
% Plot the waveform:
if size(y, 2) == 1
% Mono signal
plot(t, y, 'b-');
else
% Stereo signal. Extract just one channel for plotting.
plot(t, y(:, 1), 'b-');
end
title('Waveform', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Write signal as a .wav file.
% Create the filename where we will save the waveform.
outputFullFileName = fullfile(folder, [fieldName, '.wav']);
fprintf('Writing file %s...\n', outputFullFileName);
% Write the waveform to a file:
Fs = 44100; % Assume standard CD quality.
audiowrite(outputFullFileName, y, Fs);
% Play the sound as many times as the user wants.
playAgain = true;
counter = 1;
while playAgain
% Play the sound that we just created.
fprintf('Playing file %s %d times...\n', outputFullFileName, counter);
song = audioplayer(y, Fs);
playblocking(song);
% Ask user if they want to play the sound again.
promptMessage = sprintf('You have played the sound %d times.\nDo you want to play the sound again?', counter);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(button, 'No')
playAgain = false;
break;
end
counter = counter + 1;
end
% Alert user that we are done.
message = sprintf('Done playing %s.\n', outputFullFileName);
fprintf('%s\n', message);
promptMessage = sprintf('Done playing %s.\nClick OK to close the window\nor Cancel to leave it up.', outputFullFileName);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'OK')
close all; % Close down the figure.
end

Abdul Jelil Olawore
Abdul Jelil Olawore on 30 Jul 2017
1. Copy Audio_Bird9.mat to matlab file folder under My Document. 2. At MATLAB command window type, Load('Audio_Bird9.mat'),the Audio_Bird will appear at the workspace. 3.Type the following at the command window Fs= 44100; audiowrite ('Audio_Bird9.wav',Audio_Bird9,Fs);//loading Audio_Bird9.mat to Audio_Bird9.wav 4.Type the following at the command window Fs = 44100; [z1, Fs] = audioread ('babble.wav'); //read Audio_Bird9.wav to z1. And produce a sound z1 sound (z1,Fs); Sorry this might be really late but it can be beneficial to other looking for something similar

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!