Search code examples
matlabmachine-learningaccelerometerfeature-extractiongradient-descent

How to calculate Gradient in matlab?


I am working on pedestrian step detection (acceleration). I want to calculate statistical features from my filtered signal. I have already calculated some and now I want to calculate gradient. My data is of 1x37205 double. I calculated features using for loop with moving window size=2samples and 50% overlap of previous window. Below I am attaching the code I tried to calculate the gradient. I am not sure if it is the right way to calculate or not? In addition, I am also unable to understand that what is the purpose to use gradient, how it can be useful for step detection and how to work with gradient? Could some one guide me or provide any code help in matlab?

%%Here M is mean and V is variance i already calculated from filtered data
G = zeros(length(window:length(M)), 2);
for i = window:length(M)
temp = gradient(M(i+1-window:i),V(i+1-window:i));
G(i, 1) = temp(2, 1); % "c1"
G(i, 2) = temp(2, 1); % "c2"
end

Solution

  • One of the best features of Matlab is its documentation. If you are unfamiliar on how to get specific function documentation, enter the following in the command line:

    doc functionName
    

    Alternatively, for 'brief' documentation that displays in the command line, you can enter:

    help functionName
    

    Also see the documentation link here.

    Your question is worded poorly, so I will summarize what I understand and answer accordingly:

    • You have a step detection data (1*37205) double, let us call it stepSignal
    • stepSignal is position data
    • You want your window to be 2 steps with 50% overlap. This is the default behavior for the gradient function.

    You do not need a "for" loop to achieve your goal. According to the documentation, "gradient" can take one input.

    See the code below, and if need be add clarifications to the original question.

    %% Assume that stepSignal is already imported into the workspace
    velocity = gradient(stepSignal);
    

    One last note, when you give "gradient" two inputs, it automatically assumes the second input is a uniform spacing value.