How to recover original data after detrending? [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a vector y which contains the original data I have collected. I am using the MATLAB function detrend(..) in order to shift the data around zero and be able to compare them with another vector x which contains data which exist around zero. Now, what I want to do is to shift the data of vector y back to their original scale and also if possible shift the data of vector x as well in order to compare them around the area of the original data of y. The reason for that is because I want to compute the relative error between the data which is given by the formula:
It is obvious that x can't be around zero because it will cause division problems. I have tried to recover the data by adding to the detrending data the mean of the original data but it doesn;t give me back the original data. Is there any way I can do so ?

Detrending removes both offsets and linear trends from the data, so adding back the mean is not sufficient. You also have to add points from a line of the form d = a*t + b.
The two-output version of the detrend command returns the portion of the data, T_r that was removed from the original data:
[data_d,T_r] = detrend(___)
So unless you have access to T_r mentioned above or the equation of the line that was removed from the original data, you cannot recover it back.
You can see in MATLAB's documentation that a trend line is removed with the detrend command: https://www.mathworks.com/help/ident/ref/detrend.html

Related

Balloon string inverse kinematics [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a simulation where I have balloons bouncing around the screen with a string attached to each one. it functions like a constraint. The string should obey gravity, such that if the balloon isn't max length, there should be slack hanging down.
In order to solve this, I represent the string with line segments. I have the string start hanging straight down, and then iterate over each segment, pointing it to the balloons, and then positioning it so it is connected. This gets the behavior very close to what I want, however in order to make sure that it doesn't leave the center, i translate it back to the place it is rooted in. The problem is that the end no longer is going to be connected. If i didn't have to reset the string to the vertical position this would work, but i have to in order to make sure the slack is calculated.
I added a temporary solution of iterating over it again to reconnect it, and the behavior is decent, but it definitely isn't realistic. An example can be seen here: http://www.mikerkoval.com/simulation/balloons
click to create balloons. I am unsure how to simulate gravity. Is there a better approach to make this more realistic?
-----EDIT----
I am strugglign to get my value solved for a in the catenary function.
I defined my function as:
var k = function (a){
return Math.pow((2 * a[0]*Math.sinh(h/(2 * a[0])) - sqrt(s*s- v*v)), 2)
}
however, when I call
var sol = numeric.uncmin(k, [1]);
it runs for a little then throws a Nan error. I am trying to figure out where the problem even is, but I am having very little success since I am struggling to learn what is going on with uncmin
I think what you want is to find the parameters for the catenary curve through two points p0 and p1, the positions of the anchor and balloon, which has given arc length s, which is the length of the string.
The equation of the curve is
y = a cosh (x / a)
where a is a parameter you need to determine. Let v = y1 - y0 and h = x1 - h0. With a bit of algebra you can show the value of a must satisfy
sqrt(s*s - v*v) = 2 * a * sinh(h / (2a))
This article a nice discussion of how to solve this for a iteratively with Newton's method. The author makes a substitution of variables b = a/h that make makes the solution space close to linear, so Newton will converge very quickly to a good answer.
Newton's method is a simple iteration that requires the explicit derivative of the curve and a starting point. The article above gives the derivative. Because the curve is so close to linear, you can pick any starting point on that linear portion - b = 0.2 will do. By converging quickly, I mean the number of correct significant digits doubles with each iteration. So in practice you're likely to have all the digits of a double precision floating point number in 6 iterations or less.
Once you have the parameter, it will be a simple patter to plot the explicit curve you need.
If Newton doesn't converge in 6 iterations or so, then the instance of the problem is a very ill-conditioned one. Here that means a curve that's very nearly a straight line. So just draw such a line between p0 and p1!
All this should be easily fast enough: less expensive than your current approximation.

Why Matlab shows "Index exceeds matrix dimensions."? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my matlab code:
gg=imread('fsu_Westcott.jpg');
data1=gg(:,1);
histArray1=zeros(1,256);
x=0:1:255;
for n=1:length(data1)
histArray1(1,data1(n)+1)=histArray1(1,data1(n)+1)+1;
end
for n=1:length(data1)
number1=sum(histArray1(1:n));
end
plot(x,number1,'r')
Why it shows "Index exceeds matrix dimensions."? before I finally plot? I am new and thanks in advance! :)
On the line:
number1=sum(histArray1(1:n));
you are asking for cells 1 to n of array histArray1 but n goes from 1 to length(data1) which is larger than the length of histArray1 (256). So it is out of bounds.
This loop:
for n=1:length(data1)
number1=sum(histArray1(1:n));
end
seems unnecessary if you want to plot the histogram anyway.
One more tip, there is a function called hist which you could use to both compute the histogram and plot the result in a barchart in one line:
hist(data1(:), 0:255)
To me it looks like you have a couple things you'll want to fix. To fix the error on the line Simon pointed out, I think you'll want to have your 2nd For loop going from 1 to length(histArray) (or 256) instead of length(data1). The second problem I see is that in the 2nd For loop you aren't building an array--you're just redefining the variable number1 over and over. You should probably put number1(n)=sum(histArray1(1:n)); inside that for loop instead. This doesn't have to do with the error you're seeing but it might help you get what you want out of the script.

Matlab cosine mistake [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got some problem while equaling such function as:
function czeb()
k = 1:1:5;
Xk = cos( (pi*(k-0.5))/5);
CN(5,Xk)
end
function c = CN(N,x)
a=N*acos(x); % a is equal correct
c = cos(a); % buc c not, why?
return
end
If I view variable a inside CN function I receive
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
which is correct bun next step in CN function is to calculate cos(a).
In this step I receive incorrect value of cos(a).
It should be
cos(a) = 1.0e-04 *[-0.0367,0.1102,-0.1837,0.2571,-0.3306]
but it is 1.0e-15 * [-0.8269,-0.1837,0.3062,-0.4286,0.5511] and I don't know why...
There is a very simple explanation, and you function is not wrong.
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
is equal to pi/2 + k * pi. When you take the cosine of a, you will just get zeros. 1.0e-15 * 0.8269 is essentially zero (floating point arithmetic, and rounding errors).
I think, I understand your. Have you have create expected values manual, like cos(1.5708)? If yes, then you will always receive different results with results made by your computer. So, I think, your expected values of array a are incorrect.
First, enable long number format in the MATLAB/Octave:
format long;
After that, if you display your a array, you will see the similar output:
> a
a =
1.57079632679490 4.71238898038469 7.85398163397448 10.99557428756428 14.13716694115407
As you see at this step, your expected values and computed values by MATLAB/Octave are different. Than, if you do cos(1.57079632679490) the result will like -3.49148336110938e-15 and, as you see, this is near to the result of cos(a(1)): -8.26948102009006e-16. This means, that your stored number is different with that you see in the output.
To instead receive the same results as you expect - round your a to the fourth number after comma:
a = round(a*10000)/10000;
With this the computed results should be near to your expected results.

Matlab exercise: i just don't get it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given the signals:
f1[n] = sinc[n] {1[n+5]-1[n-5]}
f2[n] = 1-rect[n]
f3[n] = 1[n]-1[n-5]
write a programm in matlab in which you will check the following proprieties:
1)sinc[n]:=sin(phi*n)/phi*n;
2)(f1*f2)[n] = (f2*f1)[n];
3)f1[n]*{ f2[n] + f3[n] } = f1[n]*f2[n] + f1[n]*f3[n];
4)(f1*delta)[n] = (delta*f1)[n] = f1[n];
I'm really really grateful for any tips/ideal on how to solve this problem. :)
sinc[n]:=sin(phi*n)/phi*n;
That certainly isn't Matlab syntax, and the ; at the end makes it not look much like a question either. Anyway, you have two options. Either plot the functions to visually assess equivalence or else check the vectors. I'll demonstrate with this one, then you can try for all the others.
Firstly you need to make a sample n vector which will be your domain over which to test equivalence (i.e. the x values of your plot). I'm going to arbitrarily choose:
n = -10:0.01:10;
Also I'm going to assuming by phi you actually meant pi based on the Matlab definition of sinc: http://www.mathworks.com/help/signal/ref/sinc.html
So now we have to functions:
a = sinc(n);
b = sin(n)./n;
a and b are now vectors with a corresponding "y" value for each element of n. You'll also notice I used a . before the /, this means element wise divide i.e. divide each element by each corresponding element rather than matrix division which is inversion followed by matrix multiplication.
Now lets plot them:
plot(n, a, n, b, 'r')
and finally to check numerical equivalence we could do this:
all(a == b)
But (and this is probably a bit out of scope for your question but important to know) you should actually never check for absolute equivalence of floating point numbers like that as you get precision errors due to different truncations in the inner calculations (because of how your computer stores floating point numbers). So instead it is good practice to rather check that the difference between the two numbers is less than some tiny threshold.
all((a - b) < 0.000001)
I'll leave the rest up to you

Find Audio Peaks in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an audio signal of about the size 7000000 x 1. I have used the peakfinder m file in MATLAB to find the location of all of the peaks in the audio file above a specific threshold. I am now trying to find a frame sized 1000000 x 1 that contains the greatest amount of peaks. I am completely lost on how to do this and any help would be greatly appreciated. Thank you!
Well, all the peak finder function is doing is taking the second derivative and looking for any place where the resulting value is negative. This indicates a local maximum. So you can do something very similar to find any local maximum.
Once you have these indices, you can window the array containing a logical representation of the locations, and count how many peaks are there.
The code below will do what I am saying. It will window across and count the number of peaks found, and return a a vector of the counts, which you can then just find the max of, and then you have the starting index.
clc; close all; clear all;
A = randi(10,[1,100])
plot(A)
hold on
C = diff(diff(A))
indices = find(C < 0)+1;
scatter(indices,A(indices),'r')
temp = zeros(size(A));
temp(indices) = 1;
window = ones(1,5);
results = conv(temp,window,'same');
max(results)
This is of course a pet example, A would be your matrix, and window would be a matrix the length of the range you want to examine, in your case 1000000
Edit
As Try Hard has made note of in the comments below, this method will be fairly susceptible to noise, so what you can do first is run a smoothing filter over the signal before doing any derivatives, something like as follows.
filt = (1/filtLength) * ones(1,filtLength);
A = conv(A,filt,'same')
This is a simple averaging filter which will help smooth out some of the noise