Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'python.': 0.02; 'question?': 0.09; 'cc:addr:python-list': 0.11; 'posted': 0.15; 'cc:name:python list': 0.16; 'compute': 0.16; 'corresponds': 0.16; 'element:': 0.16; 'numpy': 0.16; 'peak': 0.16; 'peek': 0.16; '(you': 0.16; 'component': 0.16; 'index': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'install': 0.23; 'question': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'this.': 0.32; 'probably': 0.32; 'could': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'easily': 0.37; 'sure': 0.39; 'how': 0.40; 'duration': 0.60; 'new': 0.61; 'first': 0.61; 'maximum': 0.63; 'skip:n 10': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'lowest': 0.74; 'square': 0.74; 'wave': 0.74; 'oscar': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=LK8gUr0Xxp29CVbVCjO2knSXQC9Iad/VFafvA8vkjiM=; b=I2R2r+dMfEOE5UL7HD1UXrnUYNW42nm2HFZzgSVqSDdNBbFeubkHCkrrVE1/zJ+G9p vx6PVDFofRvCkv0oUnXF/DUShsucghxUe8XUMm/XRWKjvJuxqPlC2UHegA6MIR9VaA4P EdstPCbn/49I/9KFT2pR9DlwxQ0a/4Grxlna7PY/lET5SQTr9c0+6xUvM2Q6h3xZvzos NDen6PDUKVIYyihPVBhK7c9lzG1oWkSuH8kSecL2tIQjtM6oYDo4gsHgTjVQ/w3IKpxU E03LWhbvap00FI3SwvE3GQy0VZudRU28BGXgvHCRW1bu0zJuhlhONqxFE9FacjG4dcET yVrQ== X-Received: by 10.58.107.104 with SMTP id hb8mr5947233veb.50.1369003812970; Sun, 19 May 2013 15:50:12 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Sun, 19 May 2013 23:49:52 +0100 Subject: Re: Harmonic distortion of a input signal To: killybeard91@gmail.com Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369003821 news.xs4all.nl 15980 [2001:888:2000:d::a6]:34090 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45569 On 19 May 2013 23:25, wrote: > How can i at least find a peek in FFT spectrum of a square wave ? > From there i could easily build formula. Sorry for bothering but i am new to Python. Are you the same person who posted the original question? You probably want to use numpy for this. I'm not sure if I understand your question but here goes: First import numpy (you may need to install this first): >>> 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. Oscar