0

我正在尝试通过将收集到的一些加速器数据与也记录的 LVDT 位移进行比较来验证这些数据。为此,我试图区分 LVDT 数据两次以获得加速。但是,当我运行我的代码时,它有错误

Undefined function or variable 'DiffDiffLVDT'. 

对此进行调查,我发现 Matlab 没有处理第二个 for 循环,因此从未生成变量 DiffDiffLVDT。

为什么它会跳过第二个 for 循环?

enter code here
clear all

clc

test2 = csvread('HZ1.csv'); %raw data from excel
time = test2(:,1);
%% Filtering 
f=250;%  sampling frequency
f_cutoff = 2; % cutoff frequency
f_cutoff2 = 5;

fnorm =f_cutoff/(f/2); % normalized cut off freq, you can change it to any value depending on your requirements
fnorm2 =f_cutoff2/(f/2);

[b1,a1] = butter(4,fnorm,'low'); % Low pass Butterworth filter of order 4
[b2,a2] = butter(4,fnorm2,'low'); % Low pass Butterworth filter of order 4

filtaccZ = filtfilt(b2,a2,test2(:,6));
filtLVDTZ = filtfilt(b1,a1,test2(:,7));

%% Remove Offset

Accz = filtaccZ -mean(filtaccZ);
LVDTz = filtLVDTZ - mean(filtLVDTZ);

%% Unit Conversion

LVDTm = LVDTz/1000; % mm to m

%% LVDT Displacement to Acc

% Displacement to Velocity
for d = 2:1:(size(LVDTm)-1)

    x = LVDTm(d+1);

    y = LVDTm(d-1);

    z = x-y; %differnce in y

    a = time (d+1);

    b = time (d-1);

    c = a-b; %differnce in x

    DiffLVDT(d)= (z/c); % Displacement to velocity

end

velocity = DiffLVDT;

% Velocity to Acceleration
for e=1:1:(size(velocity)-1)
    x2 = velocity(e+1); 

    y2 = velocity(e-1);

    z2 = x2-y2; %differnce in y

    a2 = time (e+1);

    b2 = time (e-1);

    c2 = a2-b2; %differnce in x

    DiffDiffLVDT(e)= (z2/c2) %velocity to acc.

end

Acc= DiffDiffLVDT
%% Plotting
close all 
figure
hold on
plot(time(1:5000),Acc(1:5000),'b')
plot(time(1:5000),Accz(1:5000),'r')

grid on;box on
legend('DiffDiffLVDTFilter','Accz')

    enter code here
4

1 回答 1

4

因为

1:1:(size(velocity)-1)

不做你想做的事。

velocity是一个1xN数组,size(velocity)因此返回[1 N]. 冒号运算符只关心数组中的第一个值,而要循环的数组最终为空,因为

1:1:[1 N]-1 == 1:1:1-1 == 1:1:0 == Empty matrix

由于诸如此类的问题,您应该始终使用以下任一方法,而不是size(var)在构造循环时:

size(var, n) % Returns the size of the nth dimension of var
length(var) % Returns the size of the largest dimension of var
numel(var) % Returns the number of elements in var
于 2013-03-26T22:32:47.057 回答