Using an ADSR Envelope in MATLAB

  • MATLAB
  • Thread starter Divergent13
  • Start date
  • Tags
    Matlab
In summary: If you can't find the problem with that, then you may need to post more information about your code and the problem you are having.
  • #1
Divergent13
48
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
  • #2
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
 
  • #3
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.
 

1. What is an ADSR envelope?

An ADSR (Attack, Decay, Sustain, Release) envelope is a tool used in audio synthesis to shape the amplitude of a sound over time. It consists of four stages: the attack, decay, sustain, and release, which determine how quickly the sound rises to its maximum level, how long it holds that level, and how quickly it fades away.

2. How do I use an ADSR envelope in MATLAB?

To use an ADSR envelope in MATLAB, you will need to create an envelope object using the "adsr" function and specify the desired parameters for each stage (attack time, decay time, sustain level, and release time). Then, you can apply this envelope to a sound signal using the "adsrapply" function, which will modify the amplitude of the signal according to the specified envelope.

3. Can I customize the parameters of the ADSR envelope?

Yes, you can customize the parameters of the ADSR envelope according to your specific needs. You can adjust the attack, decay, sustain, and release times to change the shape and duration of the envelope, as well as the sustain level to control the overall volume of the sound. You can also apply different envelopes to different parts of a sound signal to create more complex and dynamic sounds.

4. What are some practical uses for an ADSR envelope?

An ADSR envelope can be used in a variety of audio synthesis applications, such as creating electronic music, sound effects for videos or games, and even in speech recognition and synthesis. It is a useful tool for shaping the dynamics and expression of a sound, and can add depth and variation to a simple sound signal.

5. Are there any limitations to using an ADSR envelope in MATLAB?

Using an ADSR envelope in MATLAB can be a powerful tool for audio synthesis, but there are some limitations to keep in mind. For example, the envelope can only be applied to digital sound signals, and it may not have the same level of precision and control as using a physical ADSR envelope in analog audio equipment. Additionally, the envelope may not accurately reflect the nuances and complexities of natural sounds, so it is important to use it thoughtfully and in conjunction with other techniques to achieve desired results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
707
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
18K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
Back
Top