here is the function I use. On it, I corrected the way to calculate
fwhm, and with cos, it works now, try this line : 

x=pi:0.01:3*pi; plot(x,cos(x)); fwhm(x,cos(x))

Hope it will help you

Regards, 
Christophe

Le lundi 13 juillet 2009 à 16:37 +0200, Petr Mikulik a écrit :

> > The function will not work if there is many impulsion (maximum) in the
> > vector imput. 
> > 
> > To be honest, I didn't know that cosinus had a fwhm. I just code that
> > function for my own use and share it. I will not improve this to work on
> > every mathematical function it already exist.
> 
> Well, it is necessary to test a (contributed) code into all possible inputs 
> users can supply. I've noticed that your code does neither work for 
> continuous and monotonous functions.
> 
> The enclosed script  fwhm.m  is rewritten from scratch (I was also using 
> such a function earlier).
> - It works on both vectorial and matricial input. 
> - It returns -1 if FWHM does not exist.
> - It was tested against various inputs.
> - It works on both Octave and Matlab.
> - It includes test case for Octave.
> 
> Please test it and commit to Octave if it is found OK (with whatever 
> comments in the file header).
> 
> ---
> Petr Mikulik
%% -*- texinfo -*-
%% @deftypefn {Function File} {} fwhm (@var{f})
%% @deftypefnx{Function File} {} fwhm (@var{x}, @var{f})
%% Return the full width at half maximum if a given signal.
%%
%% The full width at half maximum is an expression of the extent of a signal,
%% defined as the difference between the two extremal points of the signal where
%% it equals half of its maximum value.
%%
%% The signal is given by the vector @var{f} with optional @math{x}-values
%% @var{x}. If the latter is not given, it defaults to the indexes of @var{f}.
%%
%% If the full width at half maximum is not defined, the function returns 0.
%% @seealso{std}
%% @end deftypefn

%% This program is public domain.
%% Author: Christophe Cossou

function retval = fwhm (x, f)
  %% Check input
  if (nargin == 0)
    error ('fwhm: not enough input arguments');
  elseif (nargin == 1)
    f = x;
    x = 1:length (f);
  end

  if (numel (x) ~= numel (f))
    error ('fwhm: number of elements does not match');
  end

  %% Locate maximum
  fmax = max(f);
  fmin = min(f);
  renorm = 0.5 * (fmax-fmin) + fmin;
  f_renorm = f - renorm;

  ind = find (f_renorm(1:end-1) .* f_renorm (2:end) <= 0);

  %% If the product is negative, this means that the 'half-maximum' is between
  %% theses two values
  if (length (ind) == 2)
    %% We make a linear regression between the two values to get a more precise
    %% estimation of the fwhm. 
    x1 = (renorm - f (ind (1) + 1)) * (x (ind (1) + 1) - x (ind (1))) ...
       / (f (ind (1) + 1) - f (ind (1))) + x (ind (1) + 1);
    x2 = (renorm - f (ind (end) + 1)) * (x (ind (end) + 1) - x (ind (end))) ...
       / (f (ind (end) + 1) - f (ind (end))) + x (ind (end) + 1);
    retval = x2 - x1;
  else
    warning ('fwhm: full width at half maximum is not defined. FWHM is set to 0');
    retval = 0;
  end

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to