I am learning Matlab and now using the function chirp.
freq = 1/11025; duration = 1.5; c = 0:freq:duration;
y = chirp(c,0,150,duration)
The problem is, that it doesn't stop at 1.5. Instead it stops at 1.65 . But I don't know why.
Your interpretation of the chirp() function is not correct. Here is how you can create a fully customizable chirp function via the dsp.Chirp:
hChirp = dsp.Chirp(...
'TargetFrequency', 10, ...
'InitialFrequency', 0,...
'TargetTime', 10, ...
'SweepTime', 10, ...
'SamplesPerFrame', 10000, ...
'SampleRate', 1000);
plot(hChirp()); set(gcf, 'color', 'w'), grid on;
title('Chirp to 10 Hz')
Which gives the following output in this example:
You can refer to the documentation for further detail. This should be a more rigorous way of defining your signal.
Related
I'm trying to simulate an ARMA model and later simulate the AR and MA polynomial coefficients from the simulated time series.
I found the following ARIMA function in MatLab that I hoped help me achieve my goal.
Looking at the provided examples, I thought this would work:
AR_coef = {0.5, -0.1};
MA_coef = {0.8, 0.5, 0.3};
% simulate time series
Mdl_in = arima('AR', AR_coef, ...
'MA', MA_coef, ...
'Constant', 0, ...
'Variance', 1);
y = simulate(Mdl_in,1,'NumPaths',10000);
% estimate coefficients
Mdl_out = arima(numel(AR_coef), 0, numel(MA_coef));
T = numel(y);
idxpre = 1:Mdl_out.P;
idxest = (Mdl_out.P + 1):T;
EstMdl = estimate(Mdl_out,y(idxest)','Y0',y(idxpre)');
My hope was that EstMdl returns the AR_coef and MA_coef but it does not.
Based on the random number generation, the estimated model parameter varies a lot.
Can someone help me generate an example where I simulate an ARMA model and later estimate its coefficients?
I'm also fine with using Python if it is easier.
This is more some assistance in helping to automate chirp creation using the signal processing tools.
So basically using the chirp tool I'm trying to see if I can bulk generate 990 chirps (in a frequency range of 5-500Hz both upsweep and downsweep) so I can then do some statistics on them to define a best fit model through correlation without physically having to type out and generate all these chirps.
Example:
ch_6_60_10_lin=chirp(0:0.004:10,6,10,60,'linear',90);
To generate a chirp between 6Hz and 60Hz over a 10s interval.
I want to be able to generate 495 variables in these formats
ch_5_[5:500]_10_lin=chirp(0:0.004:10,5,10,[5:500],'linear',90) % where [5:500] are 495 steps
ch_[5:500]_500_10_lin=chirp(0:0.004:10,[5:500],10,500,'linear',90)
Any pointers in the right direction would be greatly appreciated!
Are you sure you want all of those as independent variables? It would probably be easier to manage all of this as a matrix.
tVec = 0:0.004:10; % Vector of time samples
f1Vec = 5:500; % Vector of ending frequencies for the first set of chirps
f2Vec = 5:000; % Vector of starting frequencies for the second set of chirps
nChirps1 = length(f1Vec);
nChirps2 = length(f2Vec);
nTimeSamples = length(tVec);
chirpMat1 = zeros(nChirps1, nTimeSamples);
chirpMat2 = zeros(nChirps2, nTimeSamples);
for iChirp = 1:nChirps1
chirpMat1(iChirp, :) = chirp(tVec, 5, 10, f1Vec(iChirp), 'linear', 90);
end
for iChirp = 1:nChirps2
chirpMat2(iChirp, :) = chirp(tVec, f0Vec(iChirp), 10, 500, 'linear', 90);
end
This question already has answers here:
Measure CPU time usage in Matlab (in milliseconds)
(2 answers)
Closed 6 years ago.
I am measuring the cputime taken by kmeans algorithm for each iteration using cputime feature. However, some iterations return cputime = 0. Here's my implementation:
load fisheriris;
[~,C] = kmeans(meas, 3, 'options',statset('MaxIter', 1),'Display', 'off');
results=[];
for i = 1:15
t=cputime;
[~,C] = kmeans(meas, 3, 'options',statset('MaxIter', 1),'Start',C, 'Display', 'off');
elapsedCPUTime=cputime-t;
results=[results;elapsedCPUTime];
end
This is the results I got for 15 iterations: 0, 0, 0.046875, 0, 0, 0, 0, 0, 0.03125, 0, 0, 0, 0, 0 ,0.03125. My first thought is that the computational time was too quick, hence 0 second. Is it true? If so, how can we achieve more precise cputime?
Thanks a lot.
From the documentation:
To measure performance, it is recommended that you use the timeit or
tic and toc functions. For more information, see Using tic and toc Versus the cputime Function.
Following that link:
Time a significant enough portion of code. Ideally, the code you are timing should take more than 1/10 second to run.
Try running kmeans in a loop.
load fisheriris;
[~,C] = kmeans(meas, 3, 'options',statset('MaxIter', 1),'Display', 'off');
results=[];
for ii = 1:15
t=cputime;
for k = 1:100
[~,Ctemp] = kmeans(meas, 3, 'options',statset('MaxIter', 1),'Start',C, 'Display', 'off');
end
elapsedCPUTime=(cputime-t)/100;
C = Ctemp;
results=[results;elapsedCPUTime];
end
Using i as the loop variable might yield unexpected results, which is why I changed it to ii.
i am currently doing a case study on the improved performance of a separable filter vs that of a square filter. I understand the mathematics behind the time complexity difference, however i have run into a problem with the real world implementation.
so basically what i have done is write a loop which implements my filter image function given by:
function imOut = FilterImage(imIn, kernel, boundFill, outputSize)
VkernelOffset = floor(size(kernel,1)/2);
HkernelOffset = floor(size(kernel,2)/2);
imIn = padarray(imIn, [VkernelOffset HkernelOffset], boundFill);
imInPadded = padarray(imIn, [VkernelOffset HkernelOffset], boundFill);
imOut = zeros(size(imIn));
kernelVector = reshape(kernel,1, []);
kernelVector3D = repmat(kernelVector, 1, 1, size(imIn,3));
for row = 1:size(imIn,1)
Vwindow = row + size(kernel,1)-1;
for column = 1:size(imIn,2)
Hwindow = column + size(kernel,2)-1;
imInWindowVector = reshape( ...
imInPadded(row:Vwindow, column:Hwindow, :),1,[],size(imIn,3));
imOut(row,column, :) = sum((imInWindowVector.*kernelVector3D),2);
end
end
ouputSize = lower(outputSize);
if strcmp(outputSize, 'same')
imOut = imOut((1+VkernelOffset):(size(imOut,1)-VkernelOffset), ...
(1+HkernelOffset):(size(imOut,2)-HkernelOffset), : );
elseif strcmp(outputSize, 'valid')
imOut = imOut((1+VkernelOffset*2):(size(imOut,1)-VkernelOffset*2), ...
(1+HkernelOffset*2):(size(imOut,2)-HkernelOffset*2), : );
end
end
I wrote another script which carries out the following two sets of commands on a 740x976 greyscale image and logs their processing time:
for n = 1:25
dim(n) = 6*n + 1;
h=fspecial('gaussian',dim(n), 4);
tic;
Im = FilterImage(I,h,0,'full');
tM(n) = toc;
h1 = fspecial('gaussian', [dim(n) 1], 4);
h2 = fspecial('gaussian', [1 dim(n)], 4);
tic;
It = FilterImage(I,h1,0,'full');
Is = FilterImage(It,h2,0,'full');
tS(n) = toc;
end
after plotting the respective time required i get the following result:
My problem is, Why is the separable method slower up to kernel matrices of size 49x49, and only shows improved speed from kernel sizes of 55x55 upwards, is something wrong with my image filter code?
p.s. the image filter code was designed for 3D images to take into account colour depth, however for the speed test i am using a greyscale image converted to double using im2double.
p.s.2 so as mentioned below, for comparison i carried out the same process using MATLAB's native conv2 function, and the results where as you'd expect, and also incredibly faster...
thanks
It seems like an optimization error.
I'd use the function conv2 instead.
Let's write a sample code:
mOutputImage = conv2((vFilterCoeff.' * vFilterCoeff), mInputImage);
mOutputImageSep = conv2(vFilterCoeff, vFilterCoeff.', mInputImage);
Try those in a loop where the length of vFilterCoeff (Row Vector!!!) is getting bigger.
update us what are the result now.
I am solving a set of ODEs (dy/dt) at t=0, all initial conditions t=0 y_0=(0,0,0). Can I add some number to the y values at different times (e.g., at t=10, y1 should be added to that number; at t=20, y2 should be added to that number, etc.) and solve the equations?
Inserting large discontinuities in your ODE in the way you suggest (and the way illustrated by #macduff) can lead to less precision and longer computation times (especially with ode45 - ode15s might be a better option or at least make sure that your absolute and relative tolerances are suitable). You've effectively produced a very stiff system. If you want to add some number to the ODEs starting at a specific time keep in mind that the solver only evaluates these equations at specific points in time of its own choosing. (Don't be mislead by the fact that you can obtain fixed step-size outputs by specifying tspan as more than two elements – all of Matlab's solvers are variable step-size solvers and choose their true steps based on error criteria.)
A better option is to integrate the system piecewise and append the resultant outputs from each run together:
% t = 0 to t = 10, pass parameter a = 0 to add to ODEs
a = 0;
tspan = [0 10];
[T,Y] = ode45(#(t,y)myfun(t,y,a),tspan,y_0);
% t = 10 to t = 20, pass parameter a = 10 to add to ODEs
a = 10;
[t,y] = ode45(#(t,y)myfun(t,y,a),tspan+T(end),Y(end,:));
T = [T;t(2:end)];
Y = [Y;y(2:end,:)];
% t = 20 to t = 30, pass parameter a = 20 to add to ODEs
a = 20;
[t,y] = ode45(#(t,y)myfun(t,y,a),tspan+T(end),Y(end,:));
T = [T;t(2:end)];
Y = [Y;y(2:end,:)];
The Matlab editor may complain about the array T and Y not being preallocated and/or growing, but it's fine in this case as they're growing in large chunks only a few times. Alternatively, if you want fixed output step-sizes, you can do this:
dt = 0.01;
T = 0:dt:30;
Y = zeros(length(T),length(y_0));
% t = 0 to t = 10, pass parameter a = 0 to add to ODEs
a = 0;
[~,Y(1:10/dt+1,:)] = ode45(#(t,y)myfun(t,y,a),T(1:10/dt+1),y_0);
% t = 10 to t = 20, pass parameter a = 10 to add to ODEs
a = 10;
[~,Y(10/dt+1:20/dt+1,:)] = ode45(#(t,y)myfun(t,y,a),T(10/dt+1:20/dt+1),Y(10/dt+1,:));
% t = 20 to t = 30, pass parameter a = 20 to add to ODEs
a = 20;
[~,Y(20/dt+1:end,:)] = ode45(#(t,y)myfun(t,y,a),T(20/dt+1:end),Y(20/dt+1,:));
One could easily convert both of the above blocks of code to more compact for loops if desired.
In both cases your ODE function myfun incorporates the parameter a this way:
function ydot = myfun(t,y,a)
y(1) = ... % add a however you like
...
Ok, like Simon McKenzie says we really need more info on your urgent issue, but I think I can help. From what you've given us, I'll assume you have a function myfun that you pass to something like ode45
y_0 = [0,0,0];
% here Tfinal is the time in seconds that you want to simulate to
% and specify the tspan so that you will get a solution at each whole
% number of seconds, I believe
[t,y] = ode45(#myfun,[0:0.1:Tfinal],y_0);
Somewhere you have defined your function, here I'll call it myfun
function dy = myfun(t,y)
% here let's check to see if it's time to add your offsets
% still, you may want to put a little fudge factor for the time, but
% you'll have to experiment, I'll set it up though
EPS = 1e-12;
if( t < 10 + EPS || t > 10 - EPS )
y(1) = y(1) + 10;
.
.
.
.
% this only makes sense if you then use y(1) in the compuatation
Otherwise, just add the offset to the returned solution vector, i.e.
idx10 = find( t == 10 ); % should be there since it's in the tspan
y(idx10:end) = y(idx10:end) + 10; % I guess you add it from that point on?