Average Position vs Time Graph?

I need to make a position vs time graph with data collected from a subject during walking. I have two variables, 'position' and 'time.' They are both matrices, with the position/time data from each step in its own column (position(x,y) corisponds to time(x,y)). The steps very in duration and the number of postion measurements. I normalized the time so that all steps are the same length. Right now I can produce ~30 lines showing each individual step. I need to average the data and create one line representing all the steps. Whats the best way to do this? I was told to use the spline function to create the graph but having trouble. Thanks ~Dan

2 个评论

I worked on it over the weekend but still can't figure it out... Any ideas?
Hi Daniel, i hope you managed to solve this problem i am currently working on something similar please assist with code for the position vs time graph if you do not mind

请先登录,再进行评论。

 采纳的回答

Walter Roberson
Walter Roberson 2011-6-11

1 个投票

You have 2D data, but you have not indicated which dimension you wish to average over.
It is not apparently to me why the arrays are 2D for a single subject? If multiple subjects were involved, that would make sense.
Is this really a question about producing "one line" by averaging, or is it a question about producing a smooth line from a list of (X,Y) positions? Producing one line by averaging has no immediately obvious connection to splines.

5 个评论

I am currently plotting one dimension(the vertical dimension) verse time. I have the x, y and z data. I will eventually plot position, velocity and acceleration for each dimension.
Multiple subjects are involved. I first need to average individual subjects to check for differences between two tasks. Then I will combine subjects' graphs and compare the two tasks.
I need to produce one smooth line from the data. I am unfamilar with this type of graph. I have read the help info on splines but have been unsuccessful at applying it to my data.
For your purpose, it appears you could use spline() as pretty much a drop-in for interp1()
Refine = 5;
NewX = linspace(min(OrigX), max(OrigX), Refine*length(OrigX))
NewY = spline(OrigX, OrigY, NewX);
plot(NewX, NewY)
I'm sorry, but I am still having trouble. I would like to understand a few things better before I head into the lab (~45 minute drive) and try them... I might not have internet access in the lab.
Let's say that I'm trying to plot 30 steps and each step has 20 to 40 recorded positions (varies depending on the length of the step). My variables for time and position will have 30 columns with 20 to 40 rows. 'Position' would be 'OrigY'. Would 'Time' be 'OrigX'? 'Time is a matrix... would I have to find the length of a column? Will it be able to process the variables as matrices?
I'm not sure why your variable for time would be a matrix, generally it will be a vector.
But, if you have all of your position data in one matrix where the rows indicate different positions and the columns are the time steps, you can plot this data as lines against the index values by taking the transpose. For example, suppose your data is contained in a variable named 'A':
plot(A')
will plot the data in each column as one line. Furthermore, if you have a vector of time data, you can plot A against time:
plot(t,A')
Walter...
I tried the code you provided (at least as I understand it) and received the following error:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> linspace at 22
y = [d1+(0:n-2)*(d2-d1)/(floor(n)-1) d2];
I tried several variations but received errors like:
??? Error using ==> polyfun\private\chckxy at 25
X must be a vector.
Error in ==> spline at 55
[x,y,sizey,endslopes] = chckxy(x,y);
I feel like this should be a very simple task but am having a very hard time making headway. I asked my lab for help... below is a copy of code I was given from my lab. Apparently it was used by the people that use to do the gait analysis. This is all the code I received.
N_sp =100; % desired number of samples
t_raw = 0:N_raw-1; % an example of original time set:
% (N_raw - original number of frames)
t_sp = 0: N_raw-1/(N_sp-1): N_raw-1; % desired time set
new_data = spline(t_raw,old_data,t_sp);
Any ideas?

请先登录,再进行评论。

更多回答(1 个)

If you know the model that you would like to use, and it is one equation for all of the data, then linear regression would be the way to go.
However, if you simply wish to fit a line or a polynomial, it makes it even easier; you can use the polyfit function. So, for example, say that you have t and x data that you would like to fit with a quadratic, simply type in the command
c = polyfit(t,x,2);
and you will have the coefficients of the best-fit polynomial returned in c. If you want to take it further and plot that polynomial, then use the polyval function like this:
plot(t,polyval(c,t))

8 个评论

My PI stressed using 'spline' in my code... We do not normally do the biomechanical analysis... We are this time and she would like to develope a system like the one used by the lab that normally does the biomechanical aspect of our research. I will look at using 'polyfit'... Is there any reason 'spline' would be used instead of 'polyfit'? Thanks
Does the line "curve back on itself" ? If so then polyfit() is inherently unsuitable. polyfit() also tends to give bad fits towards the extreme of the data (especially the larger values of x).
The graph looks similar to a sine wave... my 'x' values are 0 to 100... would 'spline' provide a better fit at the ends of the graph?
I need to make a graph lke Figure C...
http://i3.photobucket.com/albums/y80/griffdrc/Graph.jpg
polyfit() is unlikely to be able to give you those kinds of curves unless you had a much finer sampling interval and a much higher order polynomial (higher order ==> more numeric problems.)
For that graph, POLYFIT will not work, as Walter indicated. If you want a smooth line and you do not need a corresponding equation to the line, then create a variable tt that is more finely meshed than t, and plug it into the SPLINE function to return the corresponding x values:
tt = linspace(t(1),t(end),1e3);
xx = spline(t,x,tt);
I'm sorry, I should have stated that earlier... I DO NOT need an equation of the line.
I normalized the 'Time' data... the line 'tt = linspace(t(1),t(end),1e3);' is used to smooth the line?
xx = spline(t,x,tt);
Where t='time', x='position' and tt=smoothness of the line. I will then graph: 'plot(tt, xx)'?
Let's say that I'm trying to plot 30 steps and each step has 20 to 40 recorded positions (varies depending on the length of the step). My variables for time and position will have 30 columns with 20 to 40 rows. Will it be able to process the variables as matrices?
Laura...
You were right. I needed to make 'time' a vector. Still having some problems but making progress.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by