MATLAB: How to resample the acceleration signal

MATLAB: How to resample the acceleration signal

resampling

I have a acceleration signal as shown below.
Now I need to process this signal by resampling it at 50 Hz. How can I do it?
Your suggestions will be very valuable.
Thanks.

Best Answer

  • Hi Bijay
    I passed your image through the OCR and obtained the attached text file data2.txt there may be a few differing numbers because a few numbers turned characters after scanning, and brought back directly replacing them with best guess.
    So, let’s crack on, shall we?
    % manually removed '|' before using textscan
    format long;
    f2=fopen('data2.txt');
    % A=textscan(f1,'%f %f %f %f','Delimiter','|');
    A = textscan(f2,'%s %s %s %s');
    fclose(f2);
    L=length(A{:,1});
    x=zeros(1,L);for k=1:1:L x(k)=str2num(cell2mat(A{1}(k))); end;
    y=zeros(1,L);for k=1:1:L y(k)=str2num(cell2mat(A{2}(k))); end;
    z=zeros(1,L);for k=1:1:L z(k)=str2num(cell2mat(A{3}(k))); end;
    f=zeros(1,L); % the 'Hz' has to be removed before converting to double and ',' to '.'
    for k=1:1:L
    f_str=cell2mat(A{4}(k));
    f_str(f_str=='H')=[];f_str(f_str=='z')=[];f_str(f_str==',')='.';
    f(k)=str2num(f_str);
    end;
    T=1./f; % the time base needed to resample
    % the 1st 30 samples of vectors x y z have time intervals of 160ms between
    % adjacent samples, while 31st sample to end of sample have 20ms between
    % adjacent samples.
    % This means you only have to resample between the 1st initial 30 samples.
    % the interpolation factor is
    Q=T(1)/T(31);
    x2=x(1:30);y2=y(1:30);z2=z(1:30);
    % you may want to leave it with added zeros between samples
    x2_interp=zeros(1,30*Q);x2_interp([1:Q:end])=x2;
    % or linearly interpolate between samples
    x2_interp2=x2_interp;
    for k=1:1:29
    x_lin=linspace(x2(k),x2(k+1),Q+1);
    x2_interp2([(k-1)*Q+2:1:Q*k])=x_lin([2:end-1]);
    end
    y2_interp=zeros(1,30*Q);y2_interp([1:Q:end])=y2;
    y2_interp2=y2_interp;
    for k=1:1:29
    y_lin=linspace(y2(k),y2(k+1),Q+1);
    y2_interp2([(k-1)*Q+2:1:Q*k])=y_lin([2:end-1]);
    end
    z2_interp=zeros(1,30*Q);z2_interp([1:Q:end])=z2;
    z2_interp2=z2_interp;
    for k=1:1:29
    z_lin=linspace(z2(k),z2(k+1),Q+1);
    z2_interp2([(k-1)*Q+2:1:Q*k])=z_lin([2:end-1]);
    end
    f2_interp=zeros(1,30*Q);
    f2_interp([1:Q:end])=f2;
    f2_interp2=f2_interp;
    for k=1:1:29
    f_lin=linspace(f2(k),f2(k+1),Q+1);
    f2_interp2([(k-1)*Q+2:1:Q*k])=f_lin([2:end-1]);
    end
    % split x y z and remove the 1st part with slower sampling rates
    x([1:30])=[];
    y([1:30])=[];
    z([1:30])=[];
    f([1:30])=[];
    % and assemble x2 y2 z2 to the 2nd parts of x y z with faster sampling rate
    x=[x2_interp2 x];
    y=[y2_interp2 y];
    z=[z2_interp2 z];
    f=[f2_interp2 f];
    Please note that the acceleration would be the 2nd derivative of x, y, and z respect time, you already have the time reference with regular interval in
    T=1./f
    So if you really want the acceleration you have to apply
    diff(diff())
    to x y and z
    Bijay would you please be so kind to mark my answer as Accepted Answer?
    To any other reader, please if you find this answer of any help solving your question,
    please click on the thumbs-up vote link,
    thanks in advance
    John BG
    >
  • Add a Comment

    Your email address will not be published. Required fields are marked *