# Matlab

### From FourMs

## Contents |

## Toolboxes

- [MIDI Toolbox] by Petri Toiviainen
- [MIR Toolbox] by Olivier Lartillot
- [MoCap Toolbox] by Petri Toiviainen
- [MAT toolbox] by Bob Sturm

## Basic Matlab

Here we go through some of the basic steps in Matlab.

### Basic operations

Perform simple operation:

2+2

If using a semicolon at the end, the answer will still be calculated but not output:

2+2;

Set a variable:

a=2 b=3

Perform operation on variables:

a+b

Store in a new value

c=a+b

### Basic operators

- + Addition
- - Subtraction
- Multiplication

- / Division
- ^ Power
- ( ) Specify evaluation order

### Basic Unix commands

Find current working directory:

pwd

List directory content

ls

Move up one folder:

cd ..

Move into folder

cd folderName

### Matrices

Set a matrix:

d=[1 2 3; 4 5 6; 7 8 9]

Sum over each column:

sum(d)

Add a value to all cells:

e=d+2

Do operations on two matrices:

e+d

Get value from one cell

d(2,2)

Get value from one column: d(:,2) Get value from one row: d(2,:)

### Basic statistics

All standard statistical measures can easily be calculated in Matlab:

min(f) max(f) mean(f) median(f)

Cross-correlation of two inputs:

xcorr(a(:,2),a(:,3))

### Basic plotting

To create a new figure:

figure;

Generate a signal with 100 random values:

f=randn(100,1)

Then it is possible to use different plotting functions:

plot(f) hist(f) boxplot(f)

### Import an audio file

Import an audio file (.wav):

[y,fs,nbits]=wavread('fridans.wav')

### Export

Write matrix 'a' into a CSV file called 'piano4.csv':

csvwrite('piano4.csv',a)

### Smoothing

Simple IIR-filtering:

smooth(x)

smooth(x,'sgolay',3)

### Spectrogram

Make a spectrogram the old way:

specgram(a(:,1))

The new way:

spectrogram % do fft imagesc % to plot a spectrogram as vector graphics.

## MIRToolbox

The MIRToolbox can be installed anywhere on your system, just remember to set the path correctly (under File -> Set path). If it is installed correctly, you should be able to type the following to check the overview of functions:

help mirtoolbox

Load a sound file:

a=miraudio(‘bugge1.wav’)

To get to the actual data

mirgetdata(a);

Then it is possible to plot the waveform directly:

plot(a_d)

mirenvelope(a) mirenvelope(a,’Diff’)

mirfilterbank(a) mirfilterbank(a, ‘Bark’)

Calculate spectrum:

mirspectrum(a) mirspectrum(a,'Min',100,'Max',1000) mirspectrum(a,'Mel') mirspectrum(a,'Bark')

Use the frame message to plot a spectrogram:

mirspectrum(a,'Frame',1,'Min',100,'Max',1000,'dB') mirspectrum(a,'Frame',0.05,'Max',2000) mirspectrum(a,'Frame',0.05,'Max',3000,'dB')

Enhance by summing up values (to find harmonic components):

mirspectrum(a,'Sum','Frame',0.05,'Max',1000)

Or normalize values

mirspectrum(b,'Frame','Max',5000,'Normal')

Segmentation can be done with:

mirsegment(b) mirsegment(b,'KernelSize',32)

Autocorrelation:

mirautocor(a)

Looking at the spectral flux:

mirflux(a)