MATLAB Answers

help finding vector slope

19 views (last 30 days)
hello all,
I am trying to plot a vector named vec1, where
con = abs(g1-g2)/abs(t1:t2)*Fs));
vec1 = [g1*ones(1,Fs*t1),con*ones(1,Fs*t2), g2*ones(1,Fs*t3)];
What I want to see is a graph that has a linear slope from t1 to t2.
There must be something wrong with the con variable, but I can't get my head around it. Any help?
Thank you in advance

  1 Comment

Azzi Abdelmalek
Azzi Abdelmalek on 12 Feb 2013
what are g1,g2,...

Sign in to comment.

Accepted Answer

Youssef  Khmou
Youssef Khmou on 12 Feb 2013
Try this :
Fs=1000;
T=(0:1/Fs:20-1/Fs);
V=zeros(1,length(T));
t1=3;
t2=5;
g1=7;
g2=4;
for t=1:length(T)
if T(t)<=t1
V(t)=g1;
elseif T(t)>t1 && T(t) <=t2
V(t)=-(g1-g2)/(t2-t1)*T(t)+1.65*g1;
elseif T(t)>t2
V(t)=g2;
end
end
figure,
plot(T,V)
axis([0 20 0 10]), xlabel('time in Seconds');
ylabel(' Magnitude dB');title(' Gain')

  2 Comments

tony karamp
tony karamp on 12 Feb 2013
Thank you for your response. I have a question though... How did you derive to that? (line 16)
vec(t)=-(g1-g2)/(t2-t1)*T(t)+1.65*g1;
where did you get the 1.65 from?
Thanks
Youssef  Khmou
Youssef Khmou on 12 Feb 2013
Tony, yes i forgot to mention that :
So your function in the 2dn region is defined as : y =ax+ b with negative slope :
vect(t) = a*T(t) + constant .
The slope is a= g1-g2/(t2-t1)= -3/2 .
to derive the constant : you take the initial condition :
At t=t1 vect(t1)=g1=a*t1+constant => constant=g1-a*t1=11.5 .
you can put 11.55 instead of g1*1.65 ,the reason i did g1*1.65 is because i derived the solution before you gave the details g1,t1,.. it was an approximation rather than solution from ax+b.

Sign in to comment.

More Answers (4)

tony karamp
tony karamp on 12 Feb 2013
|
|
g1|---------------\
| \
| \
| \
g2| \---------------------
|
|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
t1 t2 t3
That's want I want to see!! Hope this helps!

  0 Comments

Sign in to comment.


the cyclist
the cyclist on 12 Feb 2013
You don't really give enough detail to diagnose this, but I did notice that in the line
con = abs(g1-g2)/abs(t1:t2)*Fs));
the parentheses are balanced, so it's not a valid MATLAB statement.

  0 Comments

Sign in to comment.


Youssef  Khmou
Youssef Khmou on 12 Feb 2013
hi, you have to declare all variables in your example like others said so as to examine the code , but now details are missing .

  0 Comments

Sign in to comment.


tony karamp
tony karamp on 12 Feb 2013
this is all I have
Fs = 1000; %sampling frequency
t1 = 3; %seconds
t2 = 5;
g1 = 7; %db
g2 = 4
from 0 to t1, the gain is equal to g1. at t1, all the way to t2, there is a linear slope, decreasing for g1 to g2. from t2 to t3 the gain is equal to g2.
That is all I have...

  0 Comments

Sign in to comment.