Using an ADSR Envelope in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter Divergent13
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on applying an ADSR (Attack, Decay, Sustain, Release) envelope to audio notes in MATLAB. The user encountered a matrix dimension mismatch error when attempting to multiply the ADSR envelope with the audio signal. A solution was provided, suggesting that the length of the audio note must match the length of the ADSR envelope. The correct approach involves defining the time vector and ensuring both the note and envelope arrays have the same number of samples.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Knowledge of audio signal processing concepts
  • Familiarity with the ADSR envelope model
  • Ability to manipulate arrays and matrices in MATLAB
NEXT STEPS
  • Learn how to use the MATLAB size function to inspect array dimensions
  • Explore MATLAB's linspace function for generating time vectors
  • Study the implementation of audio effects using envelopes in MATLAB
  • Investigate other windowing functions applicable to audio signals in MATLAB
USEFUL FOR

Audio engineers, sound designers, and MATLAB users interested in audio signal processing and envelope shaping techniques.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
19K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
5
Views
9K
  • · Replies 5 ·
Replies
5
Views
5K