Matlab spectrograms giving imaginary-like numbers - matlab

I'm using MATLAB to work with some spectrograms. I'm new to this kind of thing and come from more of a CS background than a signals background, so I'm not sure what I'm missing here although it may turn out to be fairly basic.
I'm trying to compute spectral difference, which I conceptually am pretty sure I understand. I've got a signal, I can do the SFT on it with MATLAB's spectrogram() function. Then, I try to loop over the spectrogram for the entire signal and at each sampled point compute the difference from the previous point by looping over powers at each frequency and subtracting. I thought I had the concept down, but when I try to run it, I realize that the points returned when I write "MySpectrogram(n,k)" are complex numbers, or atleast look like that. They are formed such that the first part of the number is negative, and then after it there is a +Coefficient*I (-.07+0.0061i) for example. I tried to square these results. After squaring them, they still appear as complex numbers. Now I am totally lost. Can someone explain what's happening?
I'm calling s = spectrogram(x,window,noverlap,nfft).

Here's the documentation for spectrogram
If you need to access the power spectrum, use this:
[s,f,t,ps] = spectrogram(x,window,noverlap,f,fs)

Related

Matlab: Curve Fitting with Start Value

I’m working with the Matlab Curve Fitting tool for the very first time and I have a question. My fit is exponential with two terms and it looks pretty good. The problem is, it won’t start from P(0,0), although my first measurement is.
Is it possible to force a start value onto my fit? Also, how does R-squared work? Is it safe to rely on?
Thank you so much
See a thorough description of this process here
In short, the most common method of fitting to a polynomial in matlab, polyfit, does not allow for forcing through zero (or anywhere else), and so a different function is required, lsqlin, for example.

Capturing sudden changes of data plotted in Matlab

How can I capture big changes in graph like this:
It is clear that there are 3 sudden curves which can be seen by eyes.
Can anyone help me to determine the start of each of them?
Matlab has findpeaks function in signal toolbox (I think) doing exactly this.
Without that, a primitive solution can be done by finding the derivative of the signal and checking the zero crossings for local maximums and minimums. Obviously if your signal is noisy you need to smooth it or handle false positives afterwards.

Initial guess for Transition and emission matrix Hidden Markov Model MATLAB?

From matlab HHM documentation Link:
Using hmmtrain. If you do not know the sequence of states states, but you have initial guesses for TRANS and EMIS, you can still estimate TRANS and EMIS using hmmtrain.
I cannot figure out how to find the initial guess of Transition and Emission matrix.
Else If I make a random guess, then how to tweak these matrices even after making a guess.
An initial guess is just that, a guess. You can make them whatever you want, but it's not a bad idea to try to understand how your system works first. A well-behaved (ergodic) system should be able to converge regardless of this guess given an infinite number of samples. Once you're done, you can tweak your guess and then run the code again if you'd like.
You can also try to play around with the tolerance values for the algorithm or the max number of iterations, which can be a limiting factor.

Matlab Confidence Interval for Degrees of Freedom

I would like to calculate a Confidence Interval along with my Degrees of Freedom (DOF) estimation in Matlab. I am trying to run the following line of code:
[R, DoF, ciDOF] = copulafit('t', U); % fit the copula
The code line without the "ciDOF" arguments takes between 1-3 hours to run with my data. I tried to run the code with the "ciDOF" argument several times, but the calculations seem to take very long (I stopped the calculation after 8 hours). No error message is generated.
Does anyone have experience with this argument and could kindly tell me how long I should expect the calculation to take (the size of my data is 167*19) and if I have specified the "ciDOF" argument correctly?
Many thanks for the help!
Carolin
If your data matrix U is of size 167 x 19, then what you are asking for is a copula-fit distribution dependent on 19-dimensions, making your copula a distribution in a 20-dimensional space with 19 dependent variables.
This is almost definitely why it is taking so long, because whether it is your intention or not, you are asking MATLAB to solve a minimization problem of taking 19 marginal distributions and come-up with the 19-variate joint distribution (the copula) where each marginal distribution (represented by 167 x 1 row-vectors) is uniform.
Most-likely this is a limit of the MATLAB implementation that is iterating through many independent computations and then trying to combine them together to fit the joint distribution's ideal conditions.
First and foremost -- and not to be insulting or insinuating -- you should definitely check that you really are trying to find a 19-variate copula. Also, just in case, make sure that your matrix U is oriented in the proper way, because if you have it transposed, you could be trying to ask for the solution to a 167-variate distribution.
But, if this is what you are actually trying to do, there is not really an easy way to predict how long it will take or how long it should take. Even with multiple dimensions, if your marginals are simple or uniform already, that would greatly reduce the copula computation. But, really, there is no way to tell.
Although this may seem like a cop-out, you may actually have better luck switching from MATLAB to R, especially if you have a lot of multivariate data, and you will probably find a lot more functionality in R than MATLAB. R is freely available and comes with a Graphical User Interface (GUI), in-case you aren't comfortable with command-line programming.
There are many more sources, but here is one PDF lecture on computing copula-fits in R:
http://faculty.washington.edu/ezivot/econ589/copulasPowerpoint.pdf

Efficiently visualise large quantities of points with matlab.

I have a set of 3D points which numbers up around 1 million points. I am looking to visualise these with matlab.
I have tried the following functions:
plot3
scatter3
But they are both very sluggish. Is there a more efficient way to visualise this level of points in matlab? Maybe a way to mesh the points?
If not can anyone suggest a plug-in or even a different program for visualising 3D points?
You're going to run into efficiency issues no matter what plugin/program you use if you want all million+ points to show up in a plot. My suggestion would be to downsample. Use the plot3 or scatter3 function on every other point, or every nth point, until you get a figure that is not sluggish. As long as the variance in your data isn't astronomical, downsampling a little bit shouldn't affect the overall distribution of points (given that you have a million+ points). And any software that is able to display that much data without being sluggish is most likely downsampling/binning or using some interpolation technique to do so (so you might as well have control over it).
fscatter3 from the file exchange, does what you like.
Is there a specific reason to actually have it display that many points?
I Googled around a bit and found some people who have had similar issues (someone suggested Avizo as an alternate program but I've never used it):
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308948
mathworks.com/matlabcentral/newsreader/view_thread/134022 (not clickable because I don't have enough rep to post more than two links)
An alternate solution would be to generate a histogram if you're more interested in the density of the data:
http://blogs.mathworks.com/videos/2010/01/22/advanced-making-a-2d-or-3d-histogram-to-visualize-data-density/
I you know beforehand roughly the coordinates of the feature you are looking for, try passing the cloud through a simple pass-through-filter, which essentially crops your point cloud. I.e. if you know that the feature is at a x-coordinate > 5, remove all points with x-coordinate < 5.
This filter could for the first coordinated be realized as
data = data(data(1,:) > 5,:);
Provided that your 3d data is stored in an n by 3 matrix.
This, together with downsampling, could help you out. If you still find the performance lagging, consider using something like the PCD viewer in PointCloudLibrary, check half way down the page at
http://pointclouds.org/documentation/overview/visualization.php
It is a stand alone app you could launch from matlab. I find it's performance far better than the sluggish matlab plotting tools.
For anyone who is interested I ended up finding a Point cloud visualiser called Cloud Compare. It is extremely fast and allows selection and segmentation as well as filtering on point clouds.