query regarding linspace

3 views (last 30 days)
moonman
moonman on 22 Sep 2011
the book code says
A = linspace(0, 0.9, (length(y)*0.2)); %rise 20% of signal
D = linspace(0.9, 0.8,(length(y)*0.05)); %drop of 5% of signal
S = linspace(0.8, 0.8,(length(y)*0.4)); %delay of 40% of signal
R = linspace(0.8, 0,(length(y)*0.35)); %drop of 35% of signal
y is a tone signal of music
if i change the first two values of linspace, there is no change in audio. what is purpose of linspace in this command
  1 Comment
Walter Roberson
Walter Roberson on 22 Sep 2011
You need to be careful: linspace() always includes the first and last values in the range, so if you put D right after A, you would have two 0.9 beside each other. Sometimes that is what you want, but usually it is not.

Sign in to comment.

Accepted Answer

Wayne King
Wayne King on 22 Sep 2011
Without more context, it's hard to say how they are using these vectors, but linspace() is just creating a linearly-spaced vector from A to B using the specified number of points. The spacing between points obviously depends on the starting and stopping points and the total number of samples (elements).
A = linspace(0,0.9,(length(y)*0.2));
Says "create a vector of linearly-spaced increments from 0 to 0.9 that is 20% of the length of y."
D = linspace(0.9, 0.8,(length(y)*0.05));
Create a vector that goes down from 0.9 to 0.8 using the number of points equal to 5% of the length of y.
S = linspace(0.8, 0.8,(length(y)*0.4));
The above is just a vector of the same value, 0.8, that is 40% of the length of y -- [0.8 0.8 0.8 .... ]

More Answers (6)

Wayne King
Wayne King on 22 Sep 2011
You'll have to convert the cell arrays with cell2mat(), or you'll have to do something like this.
C = cell(2,1);
C{1} = randn(1e3,1);
tone = C{1}.*ones(1e3,1);
I personally recommend that you write your own little ASDR function that accepts a vector input, so you convert from a cell array to a vector outside the function or feed the function C{i} and then inside the function create your envelope and multiply your signal by it and then return your signal as the output.
  1 Comment
moonman
moonman on 22 Sep 2011
Thanks King u really gave very good idea, i should do all this in function
i will try this and let u know

Sign in to comment.


moonman
moonman on 22 Sep 2011
This is complete code, now kindly comment
fs=8500;
y=sin(2*pi*300*(0:0.000117:3));
A = linspace(0, .9, (length(y)*0.25)); %rise 20% of signal
D = linspace(.9, 0.7,(length(y)*0.05)); %drop of 5% of signal
S = linspace(0.7, 0.7,(length(y)*0.40)); %delay of 40% of signal
R = linspace(0.7, 0,(length(y)*0.30)); %drop of 35% of signal
ADSR = [A D S R] ;
x = zeros(size(y));
x(1:length(ADSR)) = ADSR;
tone=y.* x;
Actually ADSR is an envolop which give realistic audio to pure sinewave in audio signals
My question is this how ADSR in this code is knowing that initial part of signal y will get multiplied with .25 and it will gradually rise, then decay and so on
although the audio is Ok bbut i am unable to unuderstand code
Kindly see the pic of ADSR here http://gadgetgangster.com/tutorials/390
thanks for sparing time for me

Wayne King
Wayne King on 22 Sep 2011
They have created this envelope with
fs=8500;
y=sin(2*pi*300*(0:0.000117:3));
A = linspace(0, .9, (length(y)*0.25)); %rise 20% of signal
D = linspace(.9, 0.7,(length(y)*0.05)); %drop of 5% of signal
S = linspace(0.7, 0.7,(length(y)*0.40)); %delay of 40% of signal
R = linspace(0.7, 0,(length(y)*0.30)); %drop of 35% of signal
ADSR = [A D S R] ;
Then they multiply the sine wave element by elementy by ADSR. The sine wave oscillates between [-1 1], but when you multiply it element by element by the ADSR envelope, the sine wave oscillates between positive and negative values within the shape of the envelope.

moonman
moonman on 22 Sep 2011
Ok thanks now i need one last favour. for one key this is working fine. now i want to implement ADSR on 8 keys. I tried it but having error. Without ADSR, my 8 keys are giving perfect tone but when i try to implement ADSR on 8 keys i get error
plz help me. code is written below
keys= [40 42 44 45 47 49 51 52];
%notes C D E F G A B C
dur=0.25*ones(1,length(keys));
fs=8500; %sampling frequency (it is explaied in part b)
C = cell(1, length(keys));
A= cell(1, length(keys));
D= cell(1, length(keys));
S= cell(1, length(keys));
R= cell(1, length(keys));% It will creat a cell array having one row and
% columns equal to number of keys
for i = 1:length(keys) % The for loop continues till number of Keys
C{i} = note(keys(i), dur(i)); % Function note is called which calculates
%tone for each key for the specified duration. This tone is stored in
% C{i}
A{i} = linspace(0, .9, (length(C{i})*0.25)); %rise 20% of signal
D{i} = linspace(.9, 0.7,(length(C{i})*0.05)); %drop of 5% of signal
S{i} = linspace(0.7, 0.7,(length(C{i})*0.40)); %delay of 40% of signal
R{i} = linspace(0.7, 0,(length(C{i})*0.30)); %drop of 35% of signal
end
ADSR = [A D S R] ;
x = zeros(size(C));
x(1:length(ADSR)) = ADSR;
tone=C.* x;
The error is
??? Undefined function or method 'times' for input arguments of type 'cell'.
Error in ==> attempt22 at 31 tone=C.* x;
  1 Comment
Fangjun Jiang
Fangjun Jiang on 22 Sep 2011
mis-match, C is cell array, x is double array

Sign in to comment.


moonman
moonman on 22 Sep 2011
Wayne King u r great my problem is solved

moonman
moonman on 22 Sep 2011
I would like to declare Wayne King as guru of this forum. He saved me from mess by giving an idea and my task of several hours completed in few minutes
  1 Comment
Wayne King
Wayne King on 22 Sep 2011
I'm glad you solved your problem and you're very (too) kind. There are many here who know far,far more than me and freely give so much of their time helping people. That's what makes the MATLAB user community so great. I'm amazed at the level of knowledge here, on the newsgroup, and the file exchange. And how generously they impart that knowledge to others.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!