# Using an ADSR Envelope in MATLAB

1. Oct 16, 2006

### Divergent13

Dear Members,

I am trying to apply a windowing function to a group of notes that I have created in MATLAB.

For example, a piecewise linear function that looks like this:

(No specific slopes defined).

%I first define the sampling frequency.

Fs = 8000;

%Define n.

n1 = [0:(1/Fs):(1/2)]
n2 = [0:(1/Fs):(1/4)]
n3 = [0:(1/Fs):(1)]

%Create the notes.

note1 = sin(2*pi*220*n1);
note2 = sin(2*pi*220*n2);
note3 = sin(2*pi*(220*(2^(7/12))*n2);
note3 = sin(2*pi*(220*(2^(7/12)))*n2);
note4 = sin(2*pi*(220*(2^(2/12)))*n2);
note5 = sin(2*pi*(220*(2^(3/12)))*n2);
note6 = sin(2*pi*220*n3);

%Generate 0.25 second rest.

rest = zeros(1,(Fs/4));

%Generate song.

song = [note1 rest note2 rest note3 rest note3 rest note3 rest note4 rest note5 rest note4 rest note6 rest];

This part works... and the song is heard with the appropriate rest.

Now, if I want to apply an ADSR envelope to a note, I tried this:

A = linspace(0, 1, 0.1*(Fs));
D = linspace(1, 0.8, 0.15*(Fs));
S = linspace(0.8, 0.8, 0.6*(Fs));
R = linspace(0.8, 0, 0.15*(Fs));

%I then concatenate

ADSR = [A D S R];

%Then I try applying the envelope to a note

But I am given an error saying the Matrix dimensions do not agree. Does anyone happen to know how I can fix this problem? I am rather new to Matlab in general so I am having difficulty using any other command to properly apply the ADSR envelope with the note.

Last edited: Oct 16, 2006
2. Mar 5, 2007

### mwnciflewog619

Overlooked

Hi, I just want to let you know your MATLAB skills arent bad at all, it seems that you have overlooked the size of the note1 array. To multiply or apply the envelope to the note either the envelope has to be adjusted to be the same length as the note or vice versa. In your case I would make the n value for the note equal to the envelope length. I played around with the code and made a 8000 sample long 200Hz sine wave and applied the envelope to it and it worked fine

Fs=7999
t=0:1/Fs:1
gives 8000 time points.
note=sin(2*pi*200*t)
gives 8000 amplitude points
then multiply this by your envelope which is also 8000 samples
and gives you a shaped note.

I hope this is what you were looking for, if it isnt thanks for giving me something to do for a few minutes.

Kind regards
Mathew

3. Mar 6, 2007

### electricsheep

Whenever you suspect a dimension mismatch issue, you can always inspect the dimensions of the matrices in question in the workspace window, or using the size function. That should narrow the problem down to a specific matrix.