I am trying to plot the magnitude ratio of a first order system using cftool, I'm aware there are other ways to do that but I need to get to the solution through this method.
I have simulated an RC circuit and, after having applied a sine input at several frequencies, I have measured the output of the system;
Here are the vectors I have created in MATLAB with the data I have measured:
f = [1 10 100 120 130 150 160 170 1000 2000 3000 10000];
Vi = zeros(1,12);
Vi(1,:) = 1; %amplitude
Vo = [0.99 0.99 0.85 0.79 0.77 0.73 0.7 0.68 0.16 0.08 0.05 0.02]; %amplitudes
Vdb = 20*log10(Vo./Vi); %Vo converted to dB
Now, given that an RC circuit is a first order system, I know that the relationship beetween magnitude ratio and frequency can be written as:
M(omega) = 1/(sqrt(1 + (omega * tau)^2))
So, opening cftool in MATLAB, I have set:
X data: f
Y data: Vdb
Custom Equation: 1/sqrt(1 + (2*pi*a*x)^2) %omega = 2*pi*f
Using these settings, however, cftool doesn't plot what I expected to see, so I would like to figure out where my mistakes are.
I believe the Y-data should be V0, not Vdb.
If you want the curvefit for the relationship between the voltage gain in dB and the frequency, then you need to alter the custom equation.
Related
I want to know how quickly some data returns to baseline after an initial peak (here at ca x=5);
The quadratic fit looks about right (from the figures option of matlab, shown below) - but I'm looking for a concise quantification of this curve, therefore I presume the 'decay rate' of the exponential function would be one very straightforward.
Is this assumption correct?
If yes, I looked at the formula on wiki for this, and attempted shamelessly to find a solve for the time constant (but unsuccessfully so). Can someone help me out, or is this actually a not so trivial problem?
edit: I was planning to find the peak using MathWorks' findpeaks() function, and the lowest point of the curve using the 'inverse' findpeaks() (as in: -y)
%approx data values of the curves below
y= [0 0.07 0.08 0.08 0.08 0.06 0.06 0.05 0.04 0.05 0.04 0.02 0.01 0.02 0.01 0.01 0.03 0.02 0.02 0.02 0.03 0.01 0.02 0.01 0.01 0.03 0.02 0.01 0.02 0.01];
x=1:numel(y);
plot(x,y);
These are the two options I was looking for, maybe someone can elaborate / improve this answer about the differences for these approaches - for me this is good enough, thanks for the comments. Before this step, using the data provided in the example of the question, the local maximum and minimum has to be extracted, this can be done easily using findpeaks()
approach 1) requires the curve toolbox from Matlab [Source]
%Fit a Single-Term Exponential Model, copy from Mathworks documentation, all credits go there
x = (0:0.2:5)';
y = 2*exp(-0.2*x) + 0.1*randn(size(x));
f = fit(x,y,'exp1')
f =
General model Exp1:
f(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 2.021 (1.89, 2.151)
b = -0.1812 (-0.2104, -0.152)
plot(f,x,y)
or approach 2) requires the optimizaion toolbox from Matlab [Source]
%copy from Mathworks documentation, all credits go there
rng default % for reproducibility
d = linspace(0,3);
y = exp(-1.3*d) + 0.05*randn(size(d));
fun = #(r)exp(-d*r)-y;
x0 = 4;
x = lsqnonlin(fun,x0)
plot(d,y,'ko',d,exp(-x*d),'b-')
legend('Data','Best fit')
xlabel('t')
ylabel('exp(-tx)')
I have a matrix of data. I used the polarplot command in MATLAB to plot this matrix with respect to theta.
The data oscillates between 3999.20 and 4001.52 As you can see in the following plot, the order of magnitude of oscillation of data is too small to see.
How can I modify my polar plot to see small oscillations?
My code is as below:
yf=[%750 data point]
theta = 0:4*pi/749:4*pi;
rho = yf
pax = polaraxes;
polarplot(theta,rho)
pax.ThetaDir = 'counterclockwise';
pax.ThetaZeroLocation='right'
pax.ThetaColor='r'
pax.ThetaTick=[0 30 60 90 120 150 180 210 240 270 300 330 ];
pax.RDir='normal';
pax.RTick=[3999.34 3999.67 4000 4000.33 4000.66 4000.99 4001.33 ]
pax.FontSize = 12;
Desired output:
Actual output
2-axis plot
To give an example of setting the r limits as suggested by #beaker
The following code uses the same data with using rlim to set manual limits in the second example. This scales the polar axis so that it only plots values between [3999,4000], exaggerating the scale of the oscillation.
theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta) + 3999 %To approximate your data
figure;
subplot(1,2,1)
polarplot(theta,rho)
title('Automatic r-limits')
subplot(1,2,2)
polarplot(theta,rho)
rlim([3999, 4000])
title('rlim([3999, 4000])')
Something like that maybe, where you subtract the mean of the data and scale the amplitude by a factor of 10?
yf=[%750 data point]
amp = yf - mean(yf);
amp = amp*10; % choose whatever scaling factor works for you
theta = 0:4*pi/749:4*pi;
rho = mean(yf) + amp;
Without the actual data, it's difficult to say what this will look like, but the general principle should work.
I am trying to fit a distribution to a discrete dataset.
The possible outcomes are A = [1 3 4 5 9 10] with a respective probability of prob
prob = [0.2 0.15 0.1 0.05 0.35 0.15];
I have used makedist to find the distribution
pd = makedist('Multinomial','probabilities', prob);
I wonder if there is a way to include the outcomes 1 to 10 from A in the distribution, such that I can calculate the mean and the variance of the possible outcomes with var(pd), mean(pd). Up till now the mean is 3.65, but my aim is it to have mean(pd) = 5.95, which is the weighted sum of the possible outcomes . Thanks in advance.
The solution is pretty easy. The possible outcomes of a multinomial distrubution are defined by a sequence of values starting at 1 and ending at numel(prob). From this official documentation page:
Create a multinomial distribution object for a distribution with three
possible outcomes. Outcome 1 has a probability of 1/2, outcome 2 has a
probability of 1/3, and outcome 3 has a probability of 1/6.
pd = makedist('Multinomial','probabilities',[1/2 1/3 1/6])
Basically, your vector of possible outcomes includes a few values associated to a null (signally 0) probability. Thus, define your distribution as follows in order to obtain the expected result:
p = [0.20 0.00 0.15 0.10 0.05 0.00 0.00 0.00 0.35 0.15];
pd = makedist('Multinomial','probabilities',p);
mean(pd) % 5.95
var(pd) % 12.35
I would like to perform a frame based analysis on the following curve Which expresses the relation between time and concentration (x-axis: time in minutes; y-axis: concentration in Mbq):
For the above curve I would like to perform frame based sampling by splitting the curve into 19 frames:
19 frames:
4 frames : Each of 15 seconds time interval
2 frames : Each of 30 seconds time interval
2 frames : Each of 60 seconds time interval
11 frames : Each of 200 seconds time interval
I have written the following interpolation function for the frame analysis. c_t is where my signal which was expressed in the figure above is stored:
function c_i = Frame_analysis(a1,a2,a3,b1,b2,b3,td,tmax,k1,k2,k3)
t= 0:6:3000; % The original sample time, in seconds
t_i =[0:15:60,90:30:120,180:60:240,440:200:2240];% The interpolated sample time for first 4 frames of 15 second interval
K_1 = (k1*k2)/(k2+k3);
K_2 = (k1*k3)/(k2+k3);
%DV_free= k1/(k2+k3);
c_t = zeros(size(t));
ind = (t > td) & (t < tmax);
c_t(ind)= conv(((t(ind) - td) ./ (tmax - td) * (a1 + a2 + a3)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
% Y_i = interp1(t,c_t(ind), t_i); % Interpolation for first frame
ind = (t >= tmax);
c_t(ind)=conv((a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax))) + a3 * exp(-b3 * (t(ind) - tmax)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
c_i = interp1(c_t(ind),t_i);% Interpolation for Next consequtive frames
figure;
plot(t_i,c_i);
axis([0 3000 -2000 80000]);
xlabel('Time[secs]');
ylabel('concentration [Mbq]');
title('My signal');
%plot(t,c_tnp);
end
When I run the code, I have obtained a curve without any interpolation as you can see from figure expressed below:
Where have I made a mistake in my code and how can I possibly perform a better interpolation for obtaining different frames in my Output curve expressed in first figure?
Following are the input values which i have provided manually
Frame_analysis(2501,18500,65000,0.5,0.7,0.3,3,8,0.014,0.051,0.07)
There are multiple issues here, and it will be up to you to decide how to address them.
This is what I can provide:
You do not specify an x-coordinate for your plot command. That can be done, however, Matlab will use the index of the vector for the x-axis. Example: if you have
y = 1:2:6;
plot(y);
hold on;
y = 1:1:6;
plot(y);
you'll see the difference.
How does this apply to your case? You specify a vector of higher resolution (t_i, compared to t), but in your plot command you do not provide this new vector for the x-coordinate.
Your definition of c_t provides very very small values (on the order of 10^-77). And from t > 60, your output is indifferent from 0.
How does that effect your interpolation?
You specify that for the interval [0 60] you want step-sizes of 15 - that does not give you a lot of resolution:
You might want to change to something like:
t_i =[0:0.5:60,90:30:120,180:60:240,440:200:2240];
Which will give you this plot:
In either case, I do not understand why you chose a data range above 60 (all the way until 3000) for the data you are trying to plot. This explains why you do not see anything with your axis limits axis([0 3000 -2000 80000]); that by far exceed the range of y-values and obscures the non-zero data entries for small x.
I am doing experiments with different operational amplifier circuits and I need to plot my measured results onto a graph. I have two data sets:
freq1 = [.1 .2 .5 .7 1 3 4 6 10 20 35 45 60 75 90 100]; %kHz
Vo1 = [1.2 1.6 1.2 2 2 2.4 14.8 20.4 26.4 30.4 53.6 68.8 90 114 140 152]; %mV
V1 = 19.6;
Acm = Vo1/(1000*V1);
And:
freq2 = [.1 .5 1 30 60 70 85 100]; %kHz
Vo1 = [3.96 3.96 3.96 3.84 3.86 3.88 3.88 3.88]; %V
V1 = .96;
Ad = Vo1/(2*V1);
(I would show my plots but apparently I need more reps for that)
I need to plot the equation, CMRR vs freq:
CMRR = 20*log10(abs(Ad/Acm));
The size of Ad and Acm are different and the frequency points do not match up, but the boundaries of both of these is the same, 100Hz to 100kHz (x-axis). On the line of CMRR, Matlab says that Ad and Acm matrix dimensions do not agree.
How I think I would solve this is using freq1 as the x-axis for CMRR and then taking approximated points from Ad according to the value on freq1.
Or I could do function approximations of Ad and Acm and then do the divide operator on those.
I do not know how I would code up this two ideas. Any other ideas would helpful, especially simpler ones.
I would use union to get a uniform frequency axis and then use interp1 to interpolate the data points. We need to heed Martin's advice and use the ./ operator for element by element operations. Here's an example:
Acm = Vo1./(1000*V1);
Ad = Vo1./(2*V1);
freq = union(freq1,freq2)
Acmi = interp1(freq1,Acm,freq);
Adi = interp1(freq2,Ad,freq);
% test the goodness of the interpolation
figure; plot( freq1, Acm, freq, Acmi );
legend('origial A_{cm}', 'interolated A_{cm}','Location','NorthWest');
figure; plot( freq2, Ad, freq, Adi );
legend('origial A_{d}', 'interolated A_{d}');
Then plot the output:
CMRR = 20*log10(abs(Adi./Acmi));
plot( freq, CMRR )
title('Common Mode Rejection Ratio')
xlabel('Frequency(Hz)')
ylabel('CMMR (dB)')
Here's my final plot: