MATLAB Using an ADSR Envelope in MATLAB

  • Thread starter Thread starter Divergent13
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion focuses on applying an ADSR envelope to a series of notes created in MATLAB. The user successfully generates a song with defined notes and rests but encounters an error when trying to apply the ADSR envelope to one of the notes, specifically a "Matrix dimensions do not agree" error. The issue arises from a mismatch in the lengths of the ADSR envelope and the note array. A suggestion is made to ensure that the envelope and the note have the same number of samples, which can be achieved by adjusting the time vector for the note to match the envelope's length. Additionally, using the size function in MATLAB can help identify dimension mismatches. The conversation emphasizes the importance of matching array dimensions when performing element-wise operations in MATLAB.
Divergent13
Messages
48
Reaction score
0
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:

[img=http://img168.imageshack.us/img168/2058/adsrenvelopepg0.th.jpg]

(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

newnote1 = ADSR .* note1;

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.

Thank you kindly in advance for your response.
 
Last edited:
Physics news on Phys.org
Overlooked

Divergent13 said:
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:

[img=http://img168.imageshack.us/img168/2058/adsrenvelopepg0.th.jpg]

(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

newnote1 = ADSR .* note1;

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.

Thank you kindly in advance for your response.

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 isn't thanks for giving me something to do for a few minutes.

Kind regards
Mathew
 
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.
 
Back
Top