Search code examples
matlabinterpolationfrequencynanfrequency-analysis

Matlab finding natural frequency, interp1 function creates NaN values


I created the following code in order to find the natural frequencies of a test sample which is excited by use of an impact hammer and has an accelerometer attached to it. However, I got stuck at interp_accelerance_dB_first. This interpolation creates a set of NaN values and I don't know why. I find it strange since interp_accelerance worked fine. I hope someone can help me!

N = 125000;
fs = 1/(x(2)-x(1));
ts = 1/fs;
f = -fs/2:fs/(N-1):fs/2;

% Set x-axis of graph
x_max = (N-1)*ts;
x_axis=0:ts:x_max;

% find the first natural frequency between these boundaries 
First_lower_boundary = 15; 
First_upper_boundary = 30; 


Input = abs(fft(y)); %FFT input force
Output = abs(fft(o)); %FFT output acceleration

Accelerance = Output./Input;

bin_vals = [0 : N-1]; 
fax_Hz = bin_vals*fs/N; 
N_2 = ceil(N/2);

% Interpolate accelerance function in order to be able to average all accelerance functions 
Interp_accelerance = interp1(fax_Hz(1:N_2),Accelerance(1:N_2),x_axis); 


% --- Find damping ratio of first natural frequency 

% Determine the x-axis (from the boundries at the beginning of this script) 
x_axis_first_peak = First_lower_boundary:ts:First_upper_boundary;

% Accelerance function with a logarithmic scale [dB] 
Accelerance_dB_first =  20*log10(Accelerance(First_lower_boundary:First_upper_boundary));


% Interpolate the accelerance function [dB] 
Interp_accelerance_dB_first = interp1(fax_Hz(First_lower_boundary:First_upper_boundary),Accelerance_dB_first,x_axis_first_peak);

Solution

  • Hard to say for sure without knowing what x,y,o are, but generally interp1 returns NaN when you try to interpolate outside of the bounds of the data's x-axis. Append the following at the end of your code:

    [min(fax_Hz(First_lower_boundary:First_upper_boundary)),max(fax_Hz(First_lower_boundary:First_upper_boundary))]
    [min(x_axis_first_peak),max(x_axis_first_peak)]
    

    If the second segment doesn't fall inside of the first segment, then you've found your problem.

    Incidentally, I think that interp_accelerance may be susceptible to the same error, again depending on the input parameters' exact nature.