How to use Octave 4.0.0 to read and write Audio files
To read/write Audio files using Octave in Ubuntu
Octave is an open source tool that is used for converting the audio signals in various fields such as research, medical, entertainment etc. In this tutorial, we will learn how to install Octave in Ubuntu and read/write audio files using Octave is explained.
To install Octave
First add the repository for downloading the octave package.
root@linuxhelp1:~# sudo apt-add-repository ppa:octave/stable The latest version of GNU Octave built for all supported Ubuntu releases. GNU Octave is normally distributed with Ubuntu, this PPA is for you if you have a need to use a newer version of Octave than what you can already get from your installed version of Ubuntu. Simply follow the instructions below to add this PPA to your system and install the octave package. If it' s not that easy or you encounter any errors, contact the team and let us know. Much credit goes to the Debian Octave Group who maintain the official Debian packages that this work is derived from. Without their efforts to bring Octave packaging to such a high level of quality in Debian and Debian derivatives, this PPA would not be here. More info: https://launchpad.net/~octave/+archive/ubuntu/stable Press [ENTER] to continue or ctrl-c to cancel adding it gpg: keyring `/tmp/tmplkekkwjt/secring.gpg' created gpg: keyring `/tmp/tmplkekkwjt/pubring.gpg' created gpg: requesting key DCB1CB6E from hkp server keyserver.ubuntu.com gpg: /tmp/tmplkekkwjt/trustdb.gpg: trustdb created gpg: key DCB1CB6E: public key " Launchpad PPA for GNU Octave" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1) OK
Run the following command to update the newly added repository.
root@linuxhelp1:~# apt-get update
Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:5 http://ppa.launchpad.net/octave/stable/ubuntu xenial InRelease
Reading package lists... Done
Now lets install the Octave package with the help of apt-get install command.
root@linuxhelp1:~# apt-get install octave -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libpango1.0-0 libpangox-1.0-0
Use ' sudo apt autoremove' to remove them.
The following additional packages will be installed:
aglfn ca-certificates-java default-jre-headless gnuplot-data gnuplot-tex gnuplot-x11 java-common libaec0 libamd2.4.1
libarpack2 libblas-common libblas3 libcamd2.4.1 libccolamd2.9.1 libcholmod3.0.6 libcxsparse3.1.4 libfltk-gl1.3
libfltk1.3 libgfortran3 libgl2ps0 libglpk36 libgraphicsmagick++-q16-12 libgraphicsmagick-q16-3 libhdf5-10 liblapack3
liblua5.1-0 libmagick++-6.q16-5v5 liboctave3 libopenblas-base libosmesa6 libplot2c2 libpstoedit0c2a libqhull7
libqrupdate1 libqscintilla2-12v5 libqscintilla2-l10n libqt4-opengl libsz2 libtext-unidecode-perl libumfpack5.7.1
libxml-libxml-perl libxml-namespacesupport-perl libxml-sax-base-perl libxml-sax-expat-perl libxml-sax-perl
octave-common octave-info openjdk-8-jre-headless pstoedit tex-common texinfo
.
.
.
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/jjs to provide /usr/bin/jjs (jjs) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/pack200 to provide /usr/bin/pack200 (pack200) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/rmiregistry to provide /usr/bin/rmiregistry (rmiregistry) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/unpack200 to provide /usr/bin/unpack200 (unpack200) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/orbd to provide /usr/bin/orbd (orbd) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/servertool to provide /usr/bin/servertool (servertool) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/tnameserv to provide /usr/bin/tnameserv (tnameserv) in auto mode
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jexec to provide /usr/bin/jexec (jexec) in auto mode
Setting up default-jre-headless (2:1.8-56ubuntu2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
To start the octave, search and type octave from the dashboard.
The welcome page of Octave appears. Follow the on-screen instruction to install it graphically.
The home screen of the Octave appears.
To view the audio informations
Type the following command to view the audio informations for the particular file.
> > info = audioinfo (' testing.ogg' )
To read the audio file
> > file=' testing.ogg' > > [M, fs] = audioread(file)
Here,
M &rarr is the matrix of columns for the channels involved and
fs &rarr is the sampling frequency.
Other options that is used for reading audio files are.
> > [y, fs] = audioread (filename,samples) > > [y, fs] = audioread (filename, datatype) > > [y, fs] = audioread (filename, samples, datatype)
Here,
samples specifies starting and ending frames
datatype specifies the data type to return.
Example for samples
> > samples = [1, fs) > > [y, fs] = audioread (filename, samples)
Examples for datatype
> > [y,Fs] = audioread(filename,' native' )
To write the audio file
Create the ogg file, in cosine value with the sampling frequency 44100 samples per second and the file will end in 10 seconds. The frequency of the cosine signal is 440 Hz.
> > filename=' test.ogg' > > fs=44100 > > t=0:1/fs:10 > > w=2*pi*440*t > > signal=cos(w) > > audiowrite(filename, signal, fs)
Now play the file test.ogg that we have created. It will reproduce a 440 Hz tone which is equivalent to an ‘ A’ musical tone. To view the values saved in the file, use audioread function.
> > [y,fs]=audioread(' testing.ogg' )
> > player=audioplayer(y, fs, 8)
Wolfram Alpha R (programming language) Sage and it goes on.
>> player=audioplayer(y, fs, 8);