<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Dear Dennis Shea,<div class=""><span class="Apple-tab-span" style="white-space:pre">  </span>Thank you for your answer.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>1. You are right. The cut-off frequency and widow size of the low-pass filter need to be checked and changed according to the feature of the input signal.<br class=""><div><span class="Apple-tab-span" style="white-space:pre">  </span>2. Bloomfield’s unwrapped phases plus a pi (3.141593) will be consistent with your results.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>All the best,</div><div>Feng</div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 2, 2018, at 9:21 PM, Dennis Shea <<a href="mailto:shea@ucar.edu" class="">shea@ucar.edu</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">[1] re: <br class="">"In my experience, the cut off frequency of the low-pass filter (frqcut = 
0.5*frqdem) will get a wrong amplitude and a wrong phase"<br class=""><br class=""></div><div class="">Compare with <b class="">Bloomfield (2000), figure 7.11 which uses a frqcut=0.04968</b><br class=""><br class=""><a href="https://iujfk.files.wordpress.com/2013/04/bloomfield-2000-fourier-analysis-of-time-series-an-introduction-2ed.pdf" target="_blank" class="">https://iujfk.files.wordpress.<wbr class="">com/2013/04/bloomfield-2000-<wbr class="">fourier-analysis-of-time-<wbr class="">series-an-introduction-2ed.pdf</a><br class="">------<br class="">with the results of NCL's left figure (Amplitude and Unwrapped-Phase) which are derived via: <b class="">  frqcut = 0.5*frqdem</b>=0.5/11=1/22=<b class="">0.04545</b> <br class=""><br class=""><a href="http://www.ncl.ucar.edu/Applications/Images/demod_cmplx_1_lg.png" target="_blank" class="">http://www.ncl.ucar.edu/<wbr class="">Applications/Images/demod_<wbr class="">cmplx_1_lg.png</a><br class=""><br class=""></div><div class=""><br class=""></div>[a] amplitudes visually match. Bloomfield and NCL use different filters  and <i class="">slightly</i> different low-pass cutoff frequencies so 'exact' match is unlikely. <br class=""></div>[b] unwrapped phases share <b class="">almost the identical structure</b>. The magnitudes are very different. Why? I do no know. Perhaps, Bloomfield/S+ uses a different algirithm. As noted NCL uses <b class="">unwrap_phase.<br class=""><br class=""><a href="http://www.ncl.ucar.edu/Document/Functions/Contributed/unwrap_phase.shtml" target="_blank" class="">http://www.ncl.ucar.edu/<wbr class="">Document/Functions/<wbr class="">Contributed/unwrap_phase.shtml</a><br class=""></b></div>This matches Matlab's unwrap. <br class=""><br class="">======<br class=""><br class=""></div>[2] re: "<span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap"></span>Why do you think that frqcut and frqdem are not related?<br class=""><br class=""></div>(a) frqdem: Often, this is suggested by some physical principal or theory. This is used to create the time series of <i class=""><u class="">unsmoothed</u> </i>real and imaginary components.<br class=""></div><div class=""><br class=""></div>(b) frqcut: <i class=""><u class="">User</u></i> must decide on what low-pass variability is desirable for research objective(s).<br class=""><br class=""></div>This is why I included:<br class=""><p class="">
Further, as suggested by the 
<a href="http://www.uni-muenster.de/ZIV.BennoSueselbeck/s-html/helpfiles/demod.html" class=""><b class="">S-Plus demod</b></a> documentation:
</p><pre class="">    <b class="">To better understand the results of complex demodulation 
    several lowpass filters should be tried:</b> the smaller the pass band, 
    the less instantaneous in time but more specific in frequency is the result</pre><div class=""><div class=""><div class=""><div class="">======<br class=""></div><div class="">[3] re: <span class="">(i) "What do you think the dominant frequency of original signal is not equal to the demodulation frequency (frqdem)?</span><span class=""><span class=""> (ii) </span><span class="">In my opinion, the precondition for complex demodulation is that 
the dominate frequency of original signal must be equal to 
the demodulation frequency,"</span></span><span class="">"</span><span class="">In my opinion, the precondition for complex demodulation is that 
the dominate frequency of original signal must be equal to 
the demodulation frequency,"<br class=""><br class=""></span></div><div class=""><span class="">Well, in principle, any <b class="">frqdem</b> (0<frqdem<0.5) could be specified. However, if the specified  frequency is not physically important (your terminology: "dominant"), why would you want to study it?<br class=""></span></div><div class=""><span class=""><br class="">======<br class=""></span></div><div class="gmail_extra"><span class="">[4] re: </span><span class="">t<b class="">he nwt is suggested as nwt= 2*T-1</b>. <br class=""><br class="">That just refers to the ?triangle? filter they choose to use.<br class=""><br class="">=====<br class="">[5] "</span><span class=""><span class="">If the estimations of the frqcut and the nwt can be arbitrarily 
changed, how to know whether the demodulated amplitude and phase are 
right with the suitable values of freqcut and nwt.</span><b class="">" That is your job as a scientist to decide.<br class=""></b><br class="">+<br class="">++++++++++<br class=""></span></div><div class="gmail_extra"><span class="">As I noted on the 'demodulation page':<br class=""></span><br class=""><span class="">In fact, complex demodulation (like spectral analysis), could be viewed to be as much 'art' as 'science'. 
Different filters,  <em class="">frqcut</em> and <em class="">nwt must be chosen.<br class=""><br class="">===========<br class=""></em></span><br class=""><span class=""><em class=""><span class="">+++++++++<b class=""><br class=""></b></span></em></span><div class="gmail_extra"><span class="">I think it might be good for you to talk with a statistician ... which I am not!<br class=""><br class=""></span></div><div class="gmail_extra"><span class="">Good Luck<br class=""></span></div><div class="gmail_extra"><span class="">D<br class=""></span></div></div><div class="gmail_extra"><span class=""><b class=""></b></span></div><div class="gmail_extra"><span class=""><b class=""><br class=""><br class=""></b></span><div class="gmail_quote">On Mon, Jan 1, 2018 at 1:43 PM, Nai Suan <span dir="ltr" class=""><<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class="">Dear Dennis Shea,<div class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">    </span>Thank you for updating it according to my comments. Until now, the results of NCL is coincident with the example in Bloomfield (2000)’s book.</div><div class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">    </span>I still have a question about the parameter estimations of the frqcut and nwt.</div><div class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">     </span>Why do you think that frqcut and frqdem are not related? As I understand, the frqcut must be less than the frqdem. In my experience, the cut off frequency of the low-pass filter (frqcut = 0.5*frqdem) will get a wrong amplitude and a wrong phase, and a smaller cut-off frequency is necessary (frqcut = 0.25*frqdem), when the original signal has very strong other cycles, which are different with the demodulation frequency.</div><span class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">       </span>What do you think the dominant frequency of original signal is not equal to the demodulation frequency (frqdem)? Whether is this condition still suitable for the complex demodulation? In my opinion, the precondition for complex demodulation is that the dominate frequency of original signal must be equal to the demodulation frequency, because the precondition for complex demodulation supposes that is the original data contains a perturbed periodic component, whose amplitude and phase are slowly changing ones. For example, it is not suitable for the complex demodulation of sunspot number using the demodulation frequency (1/11), if the sunspot number has very stronger 30-years cycle than 11-year cycle.<br class=""></span><span class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">    </span>Moreover, the nwt is suggested as nwt= 2*T-1 in two files 'complex-demodulation.pdf</span><span class="">’ and ‘pdfHMANlJLqCE.pdf’. However, as we known the nwt depends on the type of the filter that you select. If the estimations of the frqcut and the nwt can be arbitrarily changed, how to know whether the demodulated amplitude and phase are right with the suitable values of freqcut and nwt.</span><div class=""><span class=""><span class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-tab-span" style="white-space:pre-wrap">  </span>All the best,<br class=""></span><div class="">Feng</div><div class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail-m_-4434870793298626144h5"><div class="">On Jan 1, 2018, at 8:41 PM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:</div><br class="gmail-m_-4434870793298626144m_-6556111732610160925Apple-interchange-newline"></div></div><div class=""><div class=""><div class="gmail-m_-4434870793298626144h5"><div dir="ltr" class=""><div class=""><div class=""><div class="">[1] frqdem = period/tofloat(ntim)<br class="">
        The demodulation frequency should equal to 1/period.<br class=""><br class=""></div>Yes ... I should have coded what I wrote.   :-(<br class=""><br class="">===<br class="">[2] frqcut = 0.010<br class="">
        The cut-off frequency should equal to 0.5*frqdem<br class=""><br class=""></div>No. There is relation between frqdem and frqcut. <br class=""></div>The latter is strictly related to the desired low-pass filter characteristics.<br class=""><div class="">===<br class="">[3]   nwt    = 41<br class=""><br class=""></div><div class="">This was used only because Bloomfield (1976) used that number  of weights for his least squares low pass filter.<br class=""></div><div class="">NCL's 'demod_cmplx' uses a different low-pass filter (filwgts_lanczos). I have no idea how they compare in their response functions. Likely, they are a bit different. That is why getting exact results is problematical.<br class=""><br class="">++++++++++++++++<br class=""></div><div class="">I updated the spectral analysis and complex-demodulation page:<br class=""><br class=""><a href="https://www.ncl.ucar.edu/Applications/spec.shtml" target="_blank" class="">https://www.ncl.ucar.edu/Appli<wbr class="">cations/spec.shtml</a><br class=""><br class=""></div><div class="">The sunspot demodulation example shows two figures with different cutoff frequencies. All other parameters are the same.<br class=""></div><div class="">+++++++++++++++++<br class=""><br class=""></div><div class="">re: Moreover, the updated one can not correctly run in NCL 6.4.0.<br class=""><br class=""></div><div class="">The 'unwrap_phase' I attached used a new feature of NCL: a 'true' <b class="">elseif<br class=""><br class="">    <a href="https://www.ncl.ucar.edu/future_release.shtml" target="_blank" class="">https://www.ncl.ucar.edu/futur<wbr class="">e_release.shtml</a><br class=""></b></div><div class=""><br class=""></div><div class="">===<br class=""></div><div class="">The unwrap_phase_matlab.ncl could be used.<br class=""><br class=""></div><div class="">I have attached a version of unwrap_phase that should run with 6.4.0<br class=""><br class=""></div><div class="">Good Luck<br class=""></div><div class="">D<br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Jan 1, 2018 at 12:43 AM, Nai Suan <span dir="ltr" class=""><<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Dear Dennis Shea,<br class="">
        Thank you for your updating.<br class="">
        Some assignments are still obscure in the updated one, which cause that the results are still different with Bloomfield’s book.<br class="">
        1. frqdem = period/tofloat(ntim)<br class="">
        The demodulation frequency should equal to 1/period.<br class="">
        2. frqcut = 0.010<br class="">
        The cut-off frequency should equal to 0.5*frqdem.<br class="">
        3. nwt    = 41<br class="">
        The widow size should equal to 2T-1=21.<br class="">
        Moreover, the updated one can not correctly run in NCL 6.4.0.<br class="">
        Happy new year.<br class="">
Feng<br class="">
<div class=""><div class="gmail-m_-4434870793298626144m_-6556111732610160925h5"><br class="">
<br class="">
<br class="">
<br class="">
> On Jan 1, 2018, at 1:51 AM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:<br class="">
><br class="">
> FYI:<br class="">
><br class="">
> I have updated the demodulation pages. I have added 'Complex Demodulation' descriptive text section.<br class="">
><br class="">
> <a href="https://www.ncl.ucar.edu/Applications/spec.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Appli<wbr class="">cations/spec.shtml</a><br class="">
><br class="">
> The Scripts and Images have been updated to use a new function (unwrap_phase; 6.5.0) that 'unwraps' the 'wrapped' phases returned by atan2. This replicates the results of Matlab's 'unwrap' function. FYI: I have attached the function which will available with the 6.5.0 release.<br class="">
><br class="">
> <a href="https://www.ncl.ucar.edu/Document/Functions/Contributed/unwrap_phase.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Docum<wbr class="">ent/Functions/Contributed/unwr<wbr class="">ap_phase.shtml</a><br class="">
><br class="">
> Happy New Year<br class="">
> D<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> On Wed, Dec 27, 2017 at 6:44 AM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:<br class="">
><br class="">
><br class="">
> Sent from my iPhone<br class="">
><br class="">
> Begin forwarded message:<br class="">
><br class="">
>> From: Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>><br class="">
>> Date: December 27, 2017 at 8:40:34 AM EST<br class="">
>> To: Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>><br class="">
>> Subject: Re: [ncl-talk] The question about the complex demodulation<br class="">
>><br class="">
>> Dear Dennis Shea,<br class="">
>>      Thank you for your information.<br class="">
>>      I guess that I found the real answer.<br class="">
>>      1) you are right. The calculation of the ‘frqdem’ is wrong in NCL example. The right formula is freqdem = 1/11 ; (1/central_period)<br class="">
>>      2) The real reason of the difference between NCL and Bloomfield(2000) is the use of the function ‘unwrap’. In Bloomfield (2000), the phase angles are corrected to produce the smoother phase. I used the ‘unwrap’ function in matlab (<a href="https://www.mathworks.com/help/matlab/ref/unwrap.html" rel="noreferrer" target="_blank" class="">https://www.mathworks.com/hel<wbr class="">p/matlab/ref/unwrap.html</a>).<br class="">
>>      Thank you again for your help.<br class="">
>>      All the best,<br class="">
>> Feng<br class="">
>><br class="">
>>> On Dec 21, 2017, at 5:12 AM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:<br class="">
>>><br class="">
>>> Hello,<br class="">
>>><br class="">
>>> I am going on vacation over the Christmas holidays.<br class="">
>>><br class="">
>>> The following is offline from ncl-talk.<br class="">
>>> ------------------------------<wbr class="">------------------------------<wbr class="">-----------------<br class="">
>>> Maybe you can do some investigation on your own?<br class="">
>>><br class="">
>>> Any tool can be used to performdemodulation.<br class="">
>>> Other tools offer demodulation as a function or via examples:<br class="">
>>><br class="">
>>> Matlab: <a href="http://www.mathworks.com/help/signal/ref/demod.html" rel="noreferrer" target="_blank" class="">http://www.mathworks.com/help/<wbr class="">signal/ref/demod.html</a><br class="">
>>> R:          <a href="https://gist.github.com/mike-lawrence/8565774" rel="noreferrer" target="_blank" class="">https://gist.github.com/mike-l<wbr class="">awrence/8565774</a><br class="">
>>> Python: <a href="https://currents.soest.hawaii.edu/ocn760_4/_static/complex_demod.html" rel="noreferrer" target="_blank" class="">https://currents.soest.hawaii.<wbr class="">edu/ocn760_4/_static/complex_d<wbr class="">emod.html</a><br class="">
>>> IDL:       <a href="https://github.com/callumenator/idl/blob/master/external/Harris/demod.pro" rel="noreferrer" target="_blank" class="">https://github.com/callumenat<wbr class="">or/idl/blob/master/external/Ha<wbr class="">rris/demod.pro</a><br class="">
>>><br class="">
>>> The R example is similar to NCL except it uses a Butterworth low-pass filter rather than a Lanczos filter.<br class="">
>>><br class="">
>>> Unfortunately, most 'demodulation' are described using electrical engineering/communications terminology.<br class="">
>>> ========<br class="">
>>><br class="">
>>> The steps for complex demodulation about a specific demodulation frequency ('frqdem' , 'omega=2*pi*frqdem) follow.<br class="">
>>> I have attached all the fortran codes from the book:  demoddriver actually calls the appropriate subroutines.<br class="">
>>><br class="">
>>> ----<br class="">
>>> Bloomfield's program does detrend the sunspot series.<br class="">
>>> In NCL, this is left up to the user.   I doubt very much that this has any significant effect.<br class="">
>>><br class="">
>>> The main computational parts are:<br class="">
>>><br class="">
>>> (a) Calculate the real & imaginary compoments.<br class="">
>>> Technically, a time series is multiplied by a complex exponential at the demodulation frequency. Bloomfield's fortran code:<br class="">
>>><br class="">
>>>      do i=1,n<br class="">
>>>          arg   = (i-1)*omega<br class="">
>>>          d1(i) =  x(i)*cos(arg)*2<br class="">
>>>          d2(i) = -x(i)*sin(arg)*2<br class="">
>>>       end do<br class="">
>>><br class="">
>>> (b) A low-pass filter is applied independently to the real and imaginary components from (a).<br class="">
>>> Bloomfield uses "least squares lopass filter using convergence factors.<br class="">
>>><br class="">
>>>           subroutine lopass(x,n,cutoff,ns)<br class="">
>>><br class="">
>>> (c) Calculate the amplitudes and phases using the smoothed values from (b). An excerpt from Bloomfield's fortran code:<br class="">
>>><br class="">
>>>       do i=1,n<br class="">
>>>          amp = sqrt(x(i)*x(i)+y(i)*y(i))<br class="">
>>>          phi = atan2(y(i),x(i))        ! radians<br class="">
>>>          x(i)= amp<br class="">
>>>          y(i)= phi<br class="">
>>>       end do<br class="">
>>> ------------------------------<wbr class="">------------------------------<wbr class="">------------------------------<wbr class="">------------------------------<wbr class="">------------------<br class="">
>>> Parts (a) and (c) are straightforward. NCL uses array syntax rather than 'do' loops. SPecifically:<br class="">
>>><br class="">
>>>    arg = ispan(0,ny-1,1)*frqdem*2*pi<br class="">
>>>    ARG = conform(y, arg, ndim)     ; all dimensions<br class="">
>>>                                    ; complex demodulate<br class="">
>>>    yr  =  y*cos(ARG)*2             ; 'real' series<br class="">
>>>    yi  = -y*sin(ARG)*2             ; 'imaginary'<br class="">
>>> ---<br class="">
>>>    lanczos<br class="">
>>><br class="">
>>> ;---Calculate the amplitudes and phases  (really, no need for where function<br class="">
>>>    amp = sqrt(yr^2 + yi^2)<br class="">
>>>    pha = where(amp.eq.0,  0, atan2(yi, yr))  ; pha = atan2(yi,yr)<br class="">
>>><br class="">
>>>  I am sure that lanczos. Also, in my example, I set frqdem=1.0/22  ... It should be 1.0/11<br class="">
>>><br class="">
>>> Originally, I tried to use 'subroutine lopass'. However, Bloomfield read several constants from a file. The values of these constants are not available (near as I can tell). The descriptions are:<br class="">
>>><br class="">
>>> c     np2 - a power of 2<br class="">
>>> c     nom - (np2/2*pi)*omega<br class="">
>>> c     npa  - (np2/2*pi) times the pass frequency of the filter<br class="">
>>> c     nst   - (np2/2*pi) times the stopfrequency of the filter<br class="">
>>><br class="">
>>> These are used to calculate assorted quantities earlier in the program:<br class="">
>>><br class="">
>>>   omega = 2*pi*float(nom)/float(np2)<br class="">
>>>   cutoff   = pi*float(npa+nst)/float(np2)<br class="">
>>>   ns        = np2/(nst-npa)<br class="">
>>><br class="">
>>> I had to guess at 'np2'.<br class="">
>>><br class="">
>>> Anyway, the calculated values returned by 'lopass' were just not realistic.<br class="">
>>> As a result, I decided to use NCL's lanczos filter.<br class="">
>>><br class="">
>>> <a href="https://www.ncl.ucar.edu/Document/Functions/Built-in/filwgts_lanczos.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Docum<wbr class="">ent/Functions/Built-in/filwgts<wbr class="">_lanczos.shtml</a><br class="">
>>><br class="">
>>> I am sure the filter works fine. What should 'fca' and 'fcb' be set to? I used ihp=0 (lowpass),<br class="">
>>><br class="">
>>> I have attached 2 tests.<br class="">
>>><br class="">
>>> [1]<br class="">
>>><br class="">
>>> %> ncl aTEST.ncl<br class="">
>>><br class="">
>>> Basically, the same code as on the website. NCL automatically loads all the pertinent libraries. Still, I have explicitly included the 6.4.0 'demod_cmplx' function.<br class="">
>>>       ihp    = 0   ; low pass<br class="">
>>><br class="">
>>>       fcb    = totype(-999, typeof(pi))<br class="">
>>>       wgt    = filwgts_lanczos (nwt, ihp, frqcut, fcb, nsigma)     ; nwt=41<br class="">
>>>      ;wgt    = filwgts_lanczos (nwt, ihp, frqdem, fcb, nsigma)<br class="">
>>><br class="">
>>><br class="">
>>> [2] bTest.ncl<br class="">
>>><br class="">
>>> This uses Bloomfield's fortran code via a shared object. See: <a href="https://www.ncl.ucar.edu/Document/Tools/WRAPIT.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Docum<wbr class="">ent/Tools/WRAPIT.shtml</a><br class="">
>>><br class="">
>>> &><br class="">
>>><br class="">
>>> WRAPIT demod_ncl.f<br class="">
>>><br class="">
>>> %> bTEST.ncl<br class="">
>>><br class="">
>>><br class="">
>>><br class="">
>>><br class="">
>>><br class="">
>>><br class="">
>>><br class="">
>>> On Wed, Dec 20, 2017 at 12:46 AM, Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>> wrote:<br class="">
>>> Dear Dennis Shea,<br class="">
>>>     Sorry to disturb you again.<br class="">
>>>     Do you have any idea to explain the difference of phases in Bloomfield’s book and NCL example?<br class="">
>>>     Merry Christmas!<br class="">
>>>     All the best,<br class="">
>>> Frank<br class="">
>>><br class="">
>>><br class="">
>>>> On Dec 17, 2017, at 10:47 AM, Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>> wrote:<br class="">
>>>><br class="">
>>>> Dear Dennis Shea,<br class="">
>>>>    Thank you for your huge effect.<br class="">
>>>>    I agree with you, a minus sign does not affect the amplitude but affect the phase.<br class="">
>>>><br class="">
>>>>    If I understand well, you gave me two reasons to explain the difference of the phases in the Bloomfield’s book and the NCL example.<br class="">
>>>>    1) you show that the ‘atan2' or ‘atan’ functions may effect the phase plot. I do not agree with you. It can not be resolved the ‘jump’ in the phase plot.<br class="">
>>>>    2) you think that the different lowpass filters may be a reason. I do not agree with you too. I have used eight filter methods to carry out the complex demodulation. The results show that the filter method can slightly affect the amplitude and phase, but the filter method can not be used to explain the ‘jump’ in phase plot.<br class="">
>>>><br class="">
>>>>    Anyway, there is no an obvious difference in amplitude plot between Bloomfield’s book and NCL example. My question still focus on the differences of phases using the complex demodulation between Bloomfield’s book and NCL example. How to explain the huge and obvious differences? In a word, How to deal with the obvious ‘jump’ in NCL example? The NCL code ‘demod_cmplx_nai’ can not deal with the ‘jump’ too.<br class="">
>>>><br class="">
>>>>    You implied that a 'fancy' with the phase plot in Bloomfield’s book? Do you know or guess what technique Bloomfield did to deal with "a non-pretty jump”? I agree that Bloomfield used a technique to show only 4 degrees difference from 358 degrees to 2 degrees, but the technique does not include in the two Fortran subroutines which you sent to me. Do you have any idea to reproduce the phase plot in Bloomfield’s book?<br class="">
>>>>    All the best,<br class="">
>>>> Frank<br class="">
>>>><br class="">
>>>><br class="">
>>>>> On Dec 15, 2017, at 9:56 PM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:<br class="">
>>>>><br class="">
>>>>> This is a rather long and, perhaps, confusing response. It was written episodically.<br class="">
>>>>><br class="">
>>>>> ---<br class="">
>>>>> re: "same mistake"    .... I am not sure to what you are referring. The use of a minus sign?<br class="">
>>>>><br class="">
>>>>> The minus sign does not affect the amplitudes because the quantities are squared:  amp = sqrt(x*x+y*y)<br class="">
>>>>> It does affect the phase. Also, use of atan2 or atan to determine the phase are other issues to consider.<br class="">
>>>>><br class="">
>>>>> <a href="https://www.ncl.ucar.edu/Document/Functions/Built-in/atan2.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Docum<wbr class="">ent/Functions/Built-in/atan2.s<wbr class="">html</a><br class="">
>>>>> <a href="https://www.ncl.ucar.edu/Document/Functions/Built-in/atan.shtml" rel="noreferrer" target="_blank" class="">https://www.ncl.ucar.edu/Docum<wbr class="">ent/Functions/Built-in/atan.sh<wbr class="">tml</a><br class="">
>>>>><br class="">
>>>>> PMEL:   <a href="https://www.pmel.noaa.gov/maillists/tmap/ferret_users/fu_2007/pdfHMANlJLqCE.pdf" rel="noreferrer" target="_blank" class="">https://www.pmel.noaa.gov/mai<wbr class="">llists/tmap/ferret_users/fu_20<wbr class="">07/pdfHMANlJLqCE.pdf</a><br class="">
>>>>> Kessler: <a href="https://faculty.washington.edu/kessler/generals/complex-demodulation.pdf" rel="noreferrer" target="_blank" class="">https://faculty.washington.edu<wbr class="">/kessler/generals/complex-demo<wbr class="">dulation.pdf</a><br class="">
>>>>><br class="">
>>>>> PMEL references the same source as NCL.<br class="">
>>>>> Reference: Bloomfield, P. (1976). Fourier Decomposition of Time Series, An Introduction. Wiley, New York, 258pp.<br class="">
>>>>><br class="">
>>>>> ===<br class="">
>>>>> I have attached the (slightly updated) fortran subroutines from Bloomfield  but will include here also::<br class="">
>>>>>      demod_bloom1976 (page 148)<br class="">
>>>>>      polar_bloom1976 (page 150)<br class="">
>>>>><br class="">
>>>>>  ---------------------------<br class="">
>>>>>       subroutine demod_bloom1976(x,n,omega,d1,d<wbr class="">2)<br class="">
>>>>>       implicit none<br class="">
>>>>> c<br class="">
>>>>> c Fourier Analysis of Time Series: An Introduction<br class="">
>>>>> c P. Bloomfield (1976: Wiley); p148<br class="">
>>>>> c Minor code changes to make code more 'modern'<br class="">
>>>>> c<br class="">
>>>>>       integer n                        ! INPUT<br class="">
>>>>>       real    x(n), omega, d1(n), d2(n)<br class="">
>>>>>       integer i                        ! LOCAL<br class="">
>>>>>       real    arg<br class="">
>>>>><br class="">
>>>>>       do i=1,n<br class="">
>>>>>          arg   = (i-1)*omega<br class="">
>>>>>          d1(i) =  x(i)*cos(arg)*2<br class="">
>>>>>          d2(i) = -x(i)*sin(arg)*2<br class="">
>>>>>       end do<br class="">
>>>>><br class="">
>>>>>       return<br class="">
>>>>>       end<br class="">
>>>>> c ---------------------------<br class="">
>>>>>       subroutine polar_bloom1976(x,y,n)<br class="">
>>>>>       implicit none<br class="">
>>>>> c<br class="">
>>>>> c Fourier Analysis of Time Series: An Introduction<br class="">
>>>>> c P. Bloomfield (1976: Wiley); p150<br class="">
>>>>> c Minor code changes to make code more 'modern'<br class="">
>>>>> c<br class="">
>>>>>       integer n                        ! INPUT<br class="">
>>>>>       real    x(n), y(n)<br class="">
>>>>>       integer i                        ! LOCAL<br class="">
>>>>>       real    amp, phi<br class="">
>>>>><br class="">
>>>>>       do i=1,n<br class="">
>>>>>          amp = sqrt(x(i)*x(i)+y(i)*y(i))<br class="">
>>>>>          phi = atan2(y(i),x(i))        ! radians<br class="">
>>>>>          x(i)= amp<br class="">
>>>>>          y(i)= phi<br class="">
>>>>>       end do<br class="">
>>>>><br class="">
>>>>>       return<br class="">
>>>>>       end<br class="">
>>>>> c ---------------------------<br class="">
>>>>> c Bloomfield CALL SEQUENCE<br class="">
>>>>> c<br class="">
>>>>> c omega  = (2*pi*nom)/np2<br class="">
>>>>> c cutoff = (pi*(npa+nst))/np2<br class="">
>>>>> c ns     = np2/(nst-npa)<br class="">
>>>>> c call demod_bloom1976 (x,n,omega,d1,d2) ! DEMODULATION<br class="">
>>>>> c call lopass_bloom1976(d1,cutoff,ns)<wbr class="">    ! SMOOTH COSINE TERM<br class="">
>>>>> c call lopass_bloom1976(d2,cutoff,ns)<wbr class="">    ! SMOOTH SINE TERM<br class="">
>>>>> c lim = n-2*ns<br class="">
>>>>> c call polar_bloom1976(d1,d2,lim)        ! AMPLITUDE & PHASES<br class="">
>>>>> ==============================<wbr class="">=<br class="">
>>>>> Bloomfield's book uses a different low-pass filter than NCL (Lanczos) so results are not directly ('exactly') comparable.<br class="">
>>>>> ==============================<wbr class="">=<br class="">
>>>>> NCL's  (attached) demod_cmplx uses array notation (no 'do' loops):<br class="">
>>>>><br class="">
>>>>> ...<br class="">
>>>>>    yr  =  y*cos(ARG)*2             ; 'real' series<br class="">
>>>>>    yi  = -y*sin(ARG)*2              ; 'imaginary'<br class="">
>>>>> ...<br class="">
>>>>>    wgt    = filwgts_lanczos (nwt, ihp, frqcut, fcb, nsigma)<br class="">
>>>>><br class="">
>>>>> ;---Apply filter weights to raw demodulated series (nwt/2 point 'lost; at start/end)<br class="">
>>>>><br class="">
>>>>>    yr = wgt_runave_n_Wrap ( yr, wgt, 0, 0)<br class="">
>>>>>    yi = wgt_runave_n_Wrap ( yi, wgt, 0, 0)<br class="">
>>>>><br class="">
>>>>> ;---Use the low pass filtered values; atan2 uses y/x => yi/yr<br class="">
>>>>><br class="">
>>>>>    amp  = sqrt(yr^2 + yi^2)<br class="">
>>>>>    pha  = where(amp.eq.zero, zero, atan2(yi, yr))    ; primary phase (radians)<br class="">
>>>>><br class="">
>>>>>    pha2 = conform(pha, zero, -1)                     ; secondary phase (Bloomfield)<br class="">
>>>>>    pha2 = where(pha.ge.zero, pha-pi2, pha+pi2)<br class="">
>>>>><br class="">
>>>>> ==============================<wbr class="">====<br class="">
>>>>> You could take NCL's attached 'demod_cmplx'; copy it to create your own function (say): demod_cmplx_nai<br class="">
>>>>><br class="">
>>>>> and change what you want .... for example.... remove the minus sign<br class="">
>>>>><br class="">
>>>>>    yr  =  y*cos(ARG)*2             ; 'real' series<br class="">
>>>>>    yi  =   y*sin(ARG)*2              ; 'imaginary'<br class="">
>>>>><br class="">
>>>>> Play with it.<br class="">
>>>>><br class="">
>>>>> ==============================<wbr class="">====<br class="">
>>>>> I did not do anything 'fancy' with the phase plot. Hence, there can be 'jumps' . Think 358 degrees to 2 degrees. 'We' know it is only 4 degrees different but the plotting does not. Hence, a non-pretty jump in the plot.<br class="">
>>>>><br class="">
>>>>> ==============================<wbr class="">=====<br class="">
>>>>> Archived National Institute of Standards fortran software.<br class="">
>>>>><br class="">
>>>>> DEMODU.f  (attached): Does the same thing as Bloomfield/NCL.<br class="">
>>>>> The use af AMPL and PHAS names is because this was part of a larger library that reused array space.<br class="">
>>>>><br class="">
>>>>> The following<br class="">
>>>>><br class="">
>>>>> <a href="http://www.itl.nist.gov/div898/software/datapac/DEMOD.f" rel="noreferrer" target="_blank" class="">http://www.itl.nist.gov/div898<wbr class="">/software/datapac/DEMOD.f</a><br class="">
>>>>><br class="">
>>>>> uses<br class="">
>>>>><br class="">
>>>>>       Y1(I)=X(I)*COS(6.2831853*F*AI<wbr class="">)<br class="">
>>>>>       Y2(I)=X(I)*SIN(6.2831853*F*AI<wbr class="">)<br class="">
>>>>><br class="">
>>>>> but then for phases uses atan (not atan2)<br class="">
>>>>><br class="">
>>>>>       Z(I)=ATAN(Y1(I)/Y2(I))     !  atan(yr/yi)  ... ?implicitly handles - sign?<br class="">
>>>>><br class="">
>>>>> I am done.<br class="">
>>>>><br class="">
>>>>> Cheers<br class="">
>>>>> D<br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>> On Wed, Dec 13, 2017 at 8:31 AM, Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>> wrote:<br class="">
>>>>> Dear Dennis Shea,<br class="">
>>>>>         I found a same mistake in two documents Kessler: U. Washington: Complex Demodulation and PMEL: Complex Demodulation. The first formula should be conected by a ‘plus’ sign not ’minus’ sign.<br class="">
>>>>>         All the best,<br class="">
>>>>> Feng<br class="">
>>>>><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>> > On Dec 13, 2017, at 10:02 AM, Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>> wrote:<br class="">
>>>>> ><br class="">
>>>>> > Dear Dennis Shea,<br class="">
>>>>> >       Thank you for your reply.<br class="">
>>>>> >       If I am right, the phases of the 1700-1960 sunspot numbers has a unique solution. This means Bloomfield’s 2000 (Second Edition) is wrong or the 1st complex demodulation example in NCL is wrong. I have no idea for another option.<br class="">
>>>>> >       You mentioned NCL example was translated from Bloomfield’s subroutine. Is there any possible reason that the translation has some problems?<br class="">
>>>>> >       Anyway, if you are interested, I can send Bloomfield’s 2000 book to you.<br class="">
>>>>> >       All the best,<br class="">
>>>>> > Frank<br class="">
>>>>> ><br class="">
>>>>> ><br class="">
>>>>> >> On Dec 13, 2017, at 12:15 AM, Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank" class="">shea@ucar.edu</a>> wrote:<br class="">
>>>>> >><br class="">
>>>>> >> If the low-pass filtering (Lanczos) and smoothing (weighted running average) don't explain the differences... I do not know.<br class="">
>>>>> >> I do not have Bloomfield's 1976 book readily available. That used the 1700-1960 sunspot numbers. As I recall, NCL's plots were 'identical' to those in the 1976 book.<br class="">
>>>>> >> ----<br class="">
>>>>> >> <a href="http://www.ncl.ucar.edu/Applications/spec.shtml" rel="noreferrer" target="_blank" class="">http://www.ncl.ucar.edu/Applic<wbr class="">ations/spec.shtml</a><br class="">
>>>>> >><br class="">
>>>>> >> Data Analysis: Spectral analysis, Complex Demodulation<br class="">
>>>>> >><br class="">
>>>>> >> The 1st complex demodulation example uses the 1700-1960 sunspots<br class="">
>>>>> >> <a href="http://www.ncl.ucar.edu/Applications/Scripts/demod_cmplx_1.ncl" rel="noreferrer" target="_blank" class="">http://www.ncl.ucar.edu/Applic<wbr class="">ations/Scripts/demod_cmplx_1.n<wbr class="">cl</a><br class="">
>>>>> >> ----<br class="">
>>>>> >><br class="">
>>>>> >> The 'demod_cmplx' function can be seen at:<br class="">
>>>>> >><br class="">
>>>>> >> %> less  $NCARG_ROOT/lib/ncarg/nclscrip<wbr class="">ts/csm/contributed.ncl<br class="">
>>>>> >><br class="">
>>>>> >> Basically, it is a translation (fortran-77  ===> NCL) of Bloomfield's 1976 f77 subroutine.<br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >> On Tue, Dec 12, 2017 at 4:03 AM, Nai Suan <<a href="mailto:shif422@gmail.com" target="_blank" class="">shif422@gmail.com</a>> wrote:<br class="">
>>>>> >> Dear colleague,<br class="">
>>>>> >>         I have a question about how to explain the phase of the complex demodulation. I found that the phases of the sunspot in the figures 7.9 and 7.11 (see the attachment) in Bloomfield (2000)’s book (chapter 7 complex demodulation) were totally different with the ‘demod_cmplx_1_lg.png (see the attachment)’. Which is right? I believe the original sunspots in two examples are same and the different low-pass filter methods can not be used to explain this difference. How to explain the difference?<br class="">
>>>>> >>         Hope to hear your answer.<br class="">
>>>>> >>         All the best,<br class="">
>>>>> >> Frank<br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >> ><br class="">
>>>>> >> ><br class="">
>>>>> >> ><br class="">
>>>>> >> ><br class="">
>>>>> >> ><br class="">
>>>>> >><br class="">
>>>>> >><br class="">
>>>>> >> ______________________________<wbr class="">_________________<br class="">
>>>>> >> ncl-talk mailing list<br class="">
>>>>> >> <a href="mailto:ncl-talk@ucar.edu" target="_blank" class="">ncl-talk@ucar.edu</a><br class="">
>>>>> >> List instructions, subscriber options, unsubscribe:<br class="">
>>>>> >> <a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank" class="">http://mailman.ucar.edu/mailma<wbr class="">n/listinfo/ncl-talk</a><br class="">
>>>>> ><br class="">
>>>>><br class="">
>>>>><br class="">
>>>>> <demod_cmplx.ncl><demod_bloomf<wbr class="">ield_1976.f><DEMODU_nist.f><br class="">
>>>><br class="">
>>><br class="">
>>><br class="">
>>> <sunspot_wolf.1700_1960.txt><d<wbr class="">emod_ncl.f><aTest.ncl><bTest.n<wbr class="">cl><demod_cmplx_640.ncl><br class="">
>><br class="">
><br class="">
</div></div>> <unwrap_phase.ncl><unwrap_phas<wbr class="">e_matlab.ncl><br class="">
<br class="">
</blockquote></div><br class=""></div>
</div></div><span id="gmail-m_-4434870793298626144m_-6556111732610160925cid:5AE2D1FB-EDA3-4D6A-94F5-31897809433A" class=""><unwrap_phase.ncl_640></span></div></blockquote></div><br class=""></div></div></div></div></blockquote></div><br class=""></div></div></div></div></div>
</div></blockquote></div><br class=""></div></div></body></html>