matlabderivativespectrum

Spectrum Derivative in MATLAB, the end point problem


I am trying to take the derivative of the a spectrum with 125 bands using the following lines:

dW=diff(wavelength);
dR=diff(data);
df=dR./dW;

problem is in the next step i want to compare it with original spectrum numerically and also visually by plotting, but the size of df is 124 however my original wavelength is 125. Question is do i have to remove the first or the last band? however the output of some spectral analysis software is not changing the size. taking the average of bands also does not work, it make the graph to show crazy behavior.


Solution

  • diff is basically:

    Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]
    

    which means it has to be one shorter than your input (you can't subtract something from nothing, right?).

    What you have to do of course depends on what you want to do, but the least "meaning-altering" approach (kind of keeping causality with respect to sampling times) would be to prepend your dW and dR with a single arbitrary value.

    By the way, your ratio df=dR./dW might have a lot of NaNs if dW has zeros (which happens as soon as two consecutive data values are the same).