Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'tutorial': 0.03; 'answering': 0.09; 'meaningful': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'random': 0.14; 'benjamin': 0.16; 'compute': 0.16; 'corresponds': 0.16; 'element:': 0.16; 'numpy': 0.16; 'peak': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'component': 0.16; 'index': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '>>>>': 0.31; 'operations': 0.35; 'example,': 0.37; 'question,': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'duration': 0.60; 'received:173': 0.61; 'maximum': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'lowest': 0.74; 'square': 0.74; 'wave': 0.74; 'oscar': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: Harmonic distortion of a input signal Date: Sun, 19 May 2013 19:19:55 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369005609 news.xs4all.nl 15996 [2001:888:2000:d::a6]:47523 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45572 On 5/19/2013 6:49 PM, Oscar Benjamin wrote: >>>> import numpy as np > > Create a square wave signal: > >>>> x = np.zeros(50) >>>> x[:25] = -1 >>>> x[25:] = +1 >>>> x > array([-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., > -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1., > 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., > 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) > > Compute the magnitude spectrum: > >>>> spect = abs(np.fft.fft(x)[:25]) >>>> spect > array([ 0. , 31.85194222, 0. , 10.67342282, > 0. , 6.47213595, 0. , 4.69726931, > 0. , 3.73254943, 0. , 3.13762901, > 0. , 2.7436023 , 0. , 2.47213595, > 0. , 2.28230601, 0. , 2.15105461, > 0. , 2.06487174, 0. , 2.01589594, 0. ]) > > Find the index of the maximum element: > >>>> np.argmax(spect) > 1 > > So the peak is the lowest non-zero frequency component of the DFT. In > Hz this corresponds to a frequency of 1/T where T is the duration of > the signal. While you were answering a specific question, I think the above is a nice tutorial example, because it is more meaningful than arbitrary operations applied to random data.