function [MN,MNu,MNd]=lgram_deconv(LGRAM,dB_dyn)
%
% Call [MN,MNu,MNd]=lgram_deconv(LGRAM,dB_dyn);
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if nargin<2, dB_dyn=10; end
%
if LGRAM.iidB~=-1, disp(['You must run lgram_3d with the "leave data as complex" option chosen']); return; end
% Average over time:
AVG=data_avg(LGRAM,3,[],1,0,0);
% Find max over frequency and set values 10 dB lower to NaN:
[mm,ii]=max(AVG.data,[],1);
iibad=find(AVG.data(:,1,1)<mm-dB_dyn); nbad=length(iibad), LGRAM.dims(1),
%
SP=LGRAM;
%size(repmat(LGRAM.data(ii,:,:),[LGRAM.dims(1),1,1])), LGRAM.dims,
%keyboard
% Divide by phase at fmax:
SP.data=SP.data./repmat(LGRAM.data(ii,:,:),[LGRAM.dims(1),1,1]);
SP.data=unwrap(angle(SP.data),[],3); 
SP.dlab='Phase / \pi';
SP.iidB=0; SP.clim=[max(SP.data(:)),min(SP.data(:))];
% Average phases over time:
MN=data_avg(SP,3,[],1,0,0); MN.dlab='Phase / \pi';
% Set low values to NaN:
for jbad=1:length(iibad),
    MN.data(iibad(jbad),:,:)=NaN;
end

MNu=MN; MNu.data=unwrap(MN.data); MNu.dlab='Unwrapped Phase / \pi';
MN.data=MN.data/pi; MNu.data=MNu.data/pi;
MN.clim=[min(MN.data(:)),max(MN.data(:))];
MNu.clim=[min(MNu.data(:)),max(MNu.data(:))];
%
% Compute slopes:
MNd=MNu; MNd.data=pi*[0;diff(MNu.data)]/diff(MNd.scales{1}([1,2]));
MNd.dlab='Slope - rad/Hz';
MNd.clim=[min(MNd.data(:)),max(MNd.data(:))];
view5(MNd);
%view5(SP)
