How to perform cross correlation when one discrete signal has negative samples?
For example we have :
y=[1 2 3 4 5]
h0=[7 8 9]
but the ho starts from -2.
Just do the regular cross correlation.
Afterwards set the lags vector accordingly.
Related
I'm trying to forecast a value at step n+1 using yule-walker ar approach in matlab. The problem is that when plotting my predicted values they seem to small, like the predicted signal being a scaled version of the original.
To put my problem very simple I wrote this stripped down version that inputs a ramp (instead of a signal I measured) to be forecasted.
x = [-5 -4 -3 -2 -1 0 1 2 3 4 5];
a = aryule(x,9); % uses Yule-Walker modelling
pred = filter(-a(2:end),1,x);
disp(pred(end)); % the predicted value at time n+1
The result: 3.4415 While it should be something like 6 ... Any ideas on what I could be missing here?
if I have vectorA = [1 1 -1]; and vectorB = [-1 -1 1], and vectorC = [1 1 -1]
Correlation means the similarity of two vectors.
So Correlation(vectorA,vectorB) = vectorA*vectorB = -3
Correlation(vectorA,vectorC) = 3;
That means vectorA and vectorC are similar because the correlation is high enough.
and correlation of vectorA and vectorB are more different because the correlation is low enough.
My trouble is How can I generate SIX vectors and their correlation is low enough?That means I want these two vectors so different in every element of vector.
Is there any wise way to generate or somehow? Thanks everyone.
PS: elements in vector are all +1 or -1.
If you define correlation as the Dot Product between 2 vectors then the definition is equivalent to Orthogonality of the Vectors.
In your case, your space is R ^ 3, which means you can not find more than 3 vectors which are Orthogonal to each other (Namely, has no correlation).
If you need 6 which has the lowest correlation, well it is all about their direction.
Since the direction is the issue, let's assume all of them are normalized of have a norm of 1.
Then think about the Unit Circle and just divide it into 6 direction which the angle between them is equal, just like you'd slice a Pizza.
I have 3 sets of signals, each containing 4 distinct operational states, and I have to classify the states in each signal using K-means in Matlab. The classification is done after I have smoothened the original signal using a filter. My output should be a plot of the smoothened signal with each part of the signal in a different color to denote the different operational state.
I am very new to Matlab, and this is what I have for the classification part.
numClusters = 4;
idx_1 = kmeans([X_1 smoothY_1],numClusters,'Replicates', 5);
[numDataPoints,numDimensions] = size(smoothY_1);
Colors = hsv(numClusters);
for i = 1 : numDataPoints
plot(X_1(i),smoothY_1(i),'.','Color',Colors(idx_1(i),:))
hold on
end
I have a few questions.
1) It appears to me that the kmeans function in Matlab will return a set of arbitrary cluster index in every run. For example, running the code above on the same signal twice may give me the cluster index (for 10 data points) [4 4 2 2 2 1 1 3 3 3] and [2 2 1 1 1 4 4 3 3 3], resulting in arbitrary colors denoting each state. Ideally, I would like the indices to be (somewhat) ordered and the colors to be the same for corresponding states, so that it makes sense to say "Red means Operational State 1, blue means State 2, etc". How can I synchronize this?
I have 2 pictures to illustrate this.
Set 1 and 2 are two of the datasets. Each stage of the signal is in a different color. I would like, for example, the first segment to be red, second in cyan, third in green, fourth in purple.
2) I can't seem to plot the graph using the specifier '-'. There is no output when I tried to do that, so I'm forced to use '.', which isn't what i want. How can I plot a continuous curve here?
3) Right now, I'm running K-means independently on all 3 sets of data, so there's no concept of training/test datasets. I would like to use one dataset for training and the other 2 for testing, but I don't know how to do that using K-means in Matlab. How can I do that?
ETA: I noticed that my smoothed plots are all about half the heights of my plots of the original data, e.g. the highest point in my original signal is y = 22, while the highest point in my smoothed signal is y = 11, although the shape remains the same. Is this correct?
ETA2: I realized that it seems as if what the K-means clustering did was simply divide the graph into numClusters segments (based on X_1 values) and that's it. I've tried with different values of numClusters and each gave me equally divided segments. Surely this can't be right? For instance, isn't it more likely that the long segment after the biggest spike belong to the same cluster, rather than 3 clusters? Should I be using K-means at all?
For the first question:
You can reorder your vector with
[~,~,a] = unique(a,'stable');
For the second question:
You can find all the information about the LineSpec here:
LineSpec
If you don't add a LineSpec the default option is a continuous line, as you want.
For the third question:
I don't think that you can train your kmean algorithm (due to the method) as it could be possible with an SVM, but i'm waiting for an expert opinion.
Matlab has a built-in function for calculating rank of a matrix with decimal numbers as well as finite field numbers. However if I am not wrong they calculate only the lowest rank (least of row rank and column rank). I would like to calculate only the row rank, i.e. find the number of independent rows of a matrix (finite field in my case). Is there a function or way to do this?
In linear algebra the column rank and the row rank are always equal (see proof), so just use rank
(if you're computing the the rank of a matrix over Galois fields, consider using gfrank instead, like #DanBecker suggested in his comment):
Example:
>> A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
>> rank(A)
ans =
2
Perhaps all three columns seem to be linearly independent, but they are dependent:
[1 2; 4 5] \ [3; 6]
ans =
-1
2
meaning that -1 * [1; 4] + 2 * [2; 5] = [3; 6]
Schwartz,
Two comments:
You state in a comment "The rank function works just fine in Galois fields as well!" I don't think this is correct. Consider the example given on the documentation page for gfrank:
A = [1 0 1;
2 1 0;
0 1 1];
gfrank(A,3) % gives answer 2
rank(A) % gives answer 3
But it is possible I am misunderstanding things!
You also said "How to check if the rows of a matrix are linearly independent? Does the solution I posted above seem legit to you i.e. taking each row and finding its rank with all the other rows one by one?"
I don't know why you say "find its rank with all the other rows one by one". It is possible to have a set of vectors which are pairwise linearly independent, yet linearly dependent taken as a group. Just consider the vectors [0 1], [1 0], [1 1]. No vector is a multiple of any other, yet the set is not linearly independent.
Your problem appears to be that you have a set of vector that you know are linearly independent. You add a vector to that set, and want to know whether the new set is still linearly independent. As #EitanT said, all you need to do is combine the (row) vectors into a matrix and check whether its rank (or gfrank) is equal to the number of rows. No need to do anything "one-by-one".
Since you know that the "old" set is linearly independent, perhaps there is a nice fast algorithm to check whether the new vector makes thing linearly dependent. Maybe at each step you orthogonalize the set, and perhaps that would make the process of checking for linear independence given the new vector faster. That might make an interesting question somewhere like mathoverflow.
For this type of data I want to do an interpolation like fit from one maxima to a minima and so on, so I can look for superimposed high frequencies:
I have a matrix of values such as:
a=[ 3 7 10 3 1 5 10 5 3 2 4 8 10 7 4 3 4 2 1 4 5 7 10 8 7 6 6 4 3 2];
Now I want to choose the relative and maximum and minimum values such that
a=[ 3 0 10 0 1 0 10 0 0 2 0 0 10 0 0 0 0 0 1 0 0 0 10 0 0 0 0 0 0 2];
I essentially want to fit a straight line from a(1) to a(3) and then from a(3) to a(5) and so on, and then subtract the fit from the data.
I know there is a function "detrend" that uses a breakpoint method it denotes as "bp", and that is the closest thing I found resembling my goal.
If you know of a way MATLAB can do this I will greatly appreciate it, otherwise it seems like I have to write an m-file to do it.
I think what your question is asking for is interpolation between the local minima and local maxima of a time series (what you call the "relative minimum and maximum values".)
See this similar question and add some code that does a linear interpolation between the local minima and maxima.
interp1() will do this handily. There is no need for the input points or the output points to be evenly spaced.
>> x = sort(rand(1,10));
>> y = rand(1,10);
>> plot (x,y,'r.');
>> xx = 0:0.01:1;
>> yy = interp1(x,y,xx);
>> hold on;
>> plot (xx,yy,'b-')
What I think you really want to do is decompose the signal into components based on local time scale. (that is, where the frequencies change with time). Use the empirical mode decomposition. Wavelet methods might be an alternative, but the output of the EMD is very easy to interpret visually.
Incidentally, the plain FFT won't work if applied to the entire length of a signal that is time varying - the FFT assumes a stationary (non-varying) signal.
You need to apply the Short-Time Fourier Transform, which is the FFT applied over sliding windows of the data to get a picture of frequency over time. See the spectrogram() function.
>> plot (x,y)
>> x=0:0.001:1;
>> y = chirp(x);
>> plot (x,y);
>> figure;
>> spectrogram(y);
There are issues with time vs. frequency resolution of the short-time Fourier transform that limit its application when your sampling rate is low compared to the frequency of the data. It would would be unlikely to work well for the example data you posted a picture of.