├── hann.png ├── hamming.png ├── signal.jpg ├── rectangle.png ├── sine_hann.png ├── hann_applied.jpg ├── hann_window.jpg ├── praat_golfer.png ├── sine_blackman.png ├── sine_triangle.png ├── voxin_golfer.png ├── voxin_golfer.wav ├── ola_hann_applied.jpg ├── ola-hann_vs_hamming.png ├── voxin_golfer_blackman.png ├── time_aliased_hann_applied.jpg ├── time-aliased-hann_vs_hamming.png ├── time_aliased_hann_vs_hamming.png ├── hamming.py ├── ola_hann.py ├── time_aliased_hann.py ├── LICENSE ├── README.md └── index.html /hann.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/hann.png -------------------------------------------------------------------------------- /hamming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/hamming.png -------------------------------------------------------------------------------- /signal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/signal.jpg -------------------------------------------------------------------------------- /rectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/rectangle.png -------------------------------------------------------------------------------- /sine_hann.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/sine_hann.png -------------------------------------------------------------------------------- /hann_applied.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/hann_applied.jpg -------------------------------------------------------------------------------- /hann_window.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/hann_window.jpg -------------------------------------------------------------------------------- /praat_golfer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/praat_golfer.png -------------------------------------------------------------------------------- /sine_blackman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/sine_blackman.png -------------------------------------------------------------------------------- /sine_triangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/sine_triangle.png -------------------------------------------------------------------------------- /voxin_golfer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/voxin_golfer.png -------------------------------------------------------------------------------- /voxin_golfer.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/voxin_golfer.wav -------------------------------------------------------------------------------- /ola_hann_applied.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/ola_hann_applied.jpg -------------------------------------------------------------------------------- /ola-hann_vs_hamming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/ola-hann_vs_hamming.png -------------------------------------------------------------------------------- /voxin_golfer_blackman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/voxin_golfer_blackman.png -------------------------------------------------------------------------------- /time_aliased_hann_applied.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/time_aliased_hann_applied.jpg -------------------------------------------------------------------------------- /time-aliased-hann_vs_hamming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/time-aliased-hann_vs_hamming.png -------------------------------------------------------------------------------- /time_aliased_hann_vs_hamming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waywardgeek/spectrogram/HEAD/time_aliased_hann_vs_hamming.png -------------------------------------------------------------------------------- /hamming.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ipython -pylab 2 | 3 | # Copyright 2010, Bill Cox, all rights reserved. This program may be freely 4 | # distributed under the terms of the GPL license, version 2. 5 | 6 | from pylab import * 7 | from scipy import * 8 | 9 | sampleRate = 10000 10 | 11 | def computeNoiseBandwidth(response): 12 | total = 0.0 13 | peak = -1e50 14 | for i in range(sampleRate/2): 15 | if response[i] > peak: 16 | peak = response[i] 17 | peak = peak*peak 18 | for i in range(sampleRate/2): 19 | total += response[i]*response[i]/peak 20 | return total 21 | 22 | def genResponse(freq): 23 | signal=zeros(sampleRate) 24 | for i in range(sampleRate): 25 | signal[i] = sin(2*pi*freq*i/sampleRate) 26 | 27 | window=zeros(sampleRate) 28 | for i in range(sampleRate): 29 | window[i] = 0.54 - 0.46*cos(2*pi*i/sampleRate) 30 | 31 | # Apply the window function to the signal 32 | windowedSignal = window*signal 33 | 34 | # Compute the FFT 35 | fftOut = fft(windowedSignal) 36 | return abs(fftOut[:sampleRate/2]) 37 | 38 | worstValue = -1e50 39 | worstFreq = 0.0 40 | for i in range(100): 41 | freq = 1000.0 + i/100.0 42 | response = genResponse(freq) 43 | testFreq = 1.1*freq 44 | if response[testFreq] > worstValue: 45 | worstValue = response[testFreq] 46 | worstFreq = freq 47 | print "Worst freq = %f" % worstFreq 48 | 49 | response = genResponse(worstFreq) 50 | plot(20*log10(response[900:1100])) 51 | response = genResponse(1000) 52 | B = computeNoiseBandwidth(response) 53 | print "B = %f" % B 54 | -------------------------------------------------------------------------------- /ola_hann.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ipython -pylab 2 | 3 | # Copyright 2010, Bill Cox, all rights reserved. This program may be freely 4 | # distributed under the terms of the GPL license, version 2. 5 | 6 | from pylab import * 7 | from scipy import * 8 | 9 | sampleRate = 10000 10 | 11 | def computeNoiseBandwidth(response): 12 | total = 0.0 13 | peak = -1e50 14 | for i in range(sampleRate/2): 15 | if response[i] > peak: 16 | peak = response[i] 17 | peak = peak*peak 18 | for i in range(sampleRate/2): 19 | total += response[i]*response[i] 20 | return total/peak 21 | 22 | def genResponse(freq): 23 | signal=zeros(sampleRate*2) 24 | for i in range(sampleRate*2): 25 | signal[i] = sin(2*pi*freq*i/sampleRate) 26 | 27 | window=zeros(sampleRate*2) 28 | for i in range(sampleRate*2): 29 | window[i] = (1 - cos(pi*i/sampleRate))/2 30 | 31 | # Apply the window function to the signal 32 | windowedSignal = window*signal 33 | 34 | # Now do the overlap-add 35 | fftIn = windowedSignal[:sampleRate] + windowedSignal[sampleRate:] 36 | 37 | # Compute the FFT 38 | fftOut = fft(fftIn) 39 | return abs(fftOut[:sampleRate/2]) 40 | 41 | worstValue = -1e50 42 | worstFreq = 0.0 43 | for i in range(100): 44 | freq = 1000.0 + i/100.0 45 | response = genResponse(freq) 46 | testFreq = 1.1*freq 47 | if response[testFreq] > worstValue: 48 | worstValue = response[testFreq] 49 | worstFreq = freq 50 | print "Worst freq = %f" % worstFreq 51 | 52 | response = genResponse(worstFreq) 53 | plot(20*log10(response[900:1100])) 54 | response = genResponse(1000) 55 | B = computeNoiseBandwidth(response) 56 | print "B = %f" % B 57 | -------------------------------------------------------------------------------- /time_aliased_hann.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ipython -pylab 2 | 3 | # Copyright 2010, Bill Cox, all rights reserved. This program may be freely 4 | # distributed under the terms of the GPL license, version 2. 5 | 6 | from pylab import * 7 | from scipy import * 8 | 9 | sampleRate = 10000 10 | 11 | def computeNoiseBandwidth(response): 12 | total = 0.0 13 | peak = -1e50 14 | for i in range(sampleRate/2): 15 | if response[i] > peak: 16 | peak = response[i] 17 | peak = peak*peak 18 | for i in range(sampleRate/2): 19 | total += response[i]*response[i] 20 | return total/peak 21 | 22 | def genResponse(freq): 23 | signal=zeros(sampleRate*2) 24 | for i in range(sampleRate*2): 25 | signal[i] = sin(2*pi*freq*i/sampleRate) 26 | 27 | window=zeros(sampleRate*2) 28 | for i in range(sampleRate*2): 29 | window[i] = (1 - cos(pi*i/sampleRate))/2 30 | 31 | # Apply the window function to the signal 32 | windowedSignal = window*signal 33 | 34 | # Now do the overlap-add 35 | fftIn = windowedSignal[:sampleRate] + windowedSignal[sampleRate:] 36 | 37 | # Compute the FFT 38 | fftOut = fft(fftIn) 39 | return abs(fftOut[:sampleRate/2]) 40 | 41 | worstValue = -1e50 42 | worstFreq = 0.0 43 | for i in range(100): 44 | freq = 1000.0 + i/100.0 45 | response = genResponse(freq) 46 | testFreq = 1.1*freq 47 | if response[testFreq] > worstValue: 48 | worstValue = response[testFreq] 49 | worstFreq = freq 50 | print "Worst freq = %f" % worstFreq 51 | 52 | response = genResponse(worstFreq) 53 | plot(20*log10(response[900:1100])) 54 | response = genResponse(1000) 55 | B = computeNoiseBandwidth(response) 56 | print "B = %f" % B 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pitch Synchronous Time-Aliased-Hann FFTs for Speech Recognition 2 | 3 | By Bill Cox, waywardgeek@gmail.com 4 | 5 | Intuitively, a Time-Aliased-Hann window is a sound sample that starts playing in the middle, and slowly fades to zero, while at the same time we start playing samples from beginning and slowly fade in. It is a waveform that smoothly feeds into itself. If the signal is a single pitch period of a harmonic signal, like a voice or a single note of an instrument, then this signal can be played over and over, and will sound like the note has been extended indefinitely, without noticeable distortion. This exact algorithm is commonly used in time-domain techniques used in the TD-PSOLA class of algorithms. I wrote a Linux package called "sonic", which is used for speeding up speech, and which is particularly good at low distortion for speeds of 2X and up. It's basically [PICOLA](http://keizai.yokkaichi-u.ac.jp/~ikeda/research/picola.html), but with a new algorithm that kicks in at 2X and above. Such an algorithm is needed for improving existing text-to-speech engines to provide very high speed speech for blind speed listeners. After trying various FFT and TD-PSOLA based methods, I found that PICOLA produces far less distortion in sped up speech, even compared to the popular WSOLA algorithm, and so I investigated why that is so. The answer seems to be that the pitch synchronous overlap-add step produces no spectral distortion of harmonics, and little distortion of everything else. So, I felt it might work well as a pre-process to an FFT, especially if the FFT frames are single pitch periods. I was amazed with the results. 6 | 7 | DSP algorithms developers often use [window functions](http://en.wikipedia.org/wiki/Window_function) such as the popular Hamming window to reduce spectral leakage in their FFTs. 8 | 9 | So far as I know, none of this is new, with the possible exception of applying Time-Aliased-Hann pitch synchronously to speech. A response on comp.dsp was so informative, I think its worth reposting as-is here: 10 | 11 | > It's always nice to see someone new rediscover the advantages of the independence of window size from DFT size. The approach is not new. It can be found in a number of guises. 12 | > 13 | > It has long been used as one form of FFT based polyphase filter. For examples: "polyphase DFT filterbank", "PolyDFT" and "PFT" as used in places such as: http://www.rfel.com/download/W03006-Comparison_of_FFT_and_PolyDFT_Transient= _Response.pdf 14 | > 15 | > Fred Harris presents the concept in the 'Time Domain Signal Processing with the DFT' chapter in Doug Elliot=92s 1987 book Handbook of Digital Signal Processing. He considers the name "data-wrapping" but considers that too close to the context of "wrapping" in "phase unwrapping" and suggests "data folding" instead. 16 | > 17 | > A couple more terms approaching yours are "weighted overlap-add structure" or "windowed-presum FFT" from "Understanding Digital Signal Processing, Second Edition" by Richard G. Lyons as explained in: http://www.eetimes.com/design/embedded/4007611/DSP-Tricks-Building-a-practi= cal-spectrum-analyzer 18 | > 19 | > The most complete exposition on it that I have found comes in a 300+ page opus: TIME ALIASING METHODS OF SPECTRUM ESTIMATION by Jason F Dahl A dissertation submitted to the faculty of Brigham Young University, Dept. of Mechanical Engineering, April 2003\. 305pp Available at: http://contentdm.lib.byu.edu/ETD/image/etd157.pdf 20 | > 21 | > I personally prefer Dahl's "time aliasing" as having the least conflict with other uses of the component words. 22 | > 23 | > This literature provides comparisons to other windows and window design methods. 24 | > 25 | > Dale B. Dalrymple 26 | 27 | Having now read these papers, and Jason Dahl's dissertation, I feel I have a much better understanding of time-aliased techniques and their applications. The pitch synchronous time-aliased FFT with a low leakage window like a Hanning window is excellent for speech analysis. 28 | 29 | # Definition of Time-Aliased-Hann 30 | 31 | Applying a Time-Aliased-Hann window is done by first applying a Hann window to two adjacent FFT input frames, and then adding the first frame to the second. This second step is described in various "time aliasing" techniques for DFTs, and in general is not restricted to two frames, but can be any number of input frames. However, for pitch synchronous speech processing, exactly two pitch periods works quite well. More frames make the time resolution too poor. Given two frames worth of input samples, x(n), n = 0 .. 2*N - 1, we compute the windowed samples x'(n), n = 0 .. N - 1: 32 | 33 | w(n) = (1 - cos(pi*n/N))/2, n = 0 .. 2*N - 1. 34 | x'(n) = w(n)*x(n) + w(n + N)*x(n + N), n = 0 .. N - 1 35 | 36 | Here is a plot of worst-case spectral noise for Hamming windows vs Time-Aliased-Hann windows, at about 1KHz: 37 | 38 | ![Hamming vs Time-Aliased-Hann](time_aliased_hann_vs_hamming.png) 39 | 40 | This plot was created using [time_aliased_hann.py](time_aliased_hann.py) and [hamming_py](hamming.py). This plot basically shows that we should not use Hamming windows for speech spectrograms, as is commonly done. The spectral noise is too high for high dynamic range spectrograms like these. Hann windows work better. The Time-Aliased-Hann window is exactly the same as a normal FFT response of a Hann-windowed sample of 2 pitch periods, but we drop every other frequency bin, keeping only harmonics of the fundamental pitch. 41 | 42 | # What it Looks Like 43 | 44 | Let's see an example. In this case, we're using a frame size of 500. The input signal is a worst case for Time-Aliased-Hann: 5.75 sine wave cycles per frame, neither harmonic, or anti-harmonic, but exactly in between. Here's two frames worth of input data: 45 | 46 | ![Input signal](signal.jpg) 47 | 48 | We apply a Hann window function to two frames, or 1000 samples. Here's what a Hann function looks like: 49 | 50 | ![Hann window](hann_window.jpg) 51 | 52 | We multiply the Hann function onto the input signal to get: 53 | 54 | ![Hann window applied to input signal](hann_applied.jpg) 55 | 56 | Finally, we take the first 500 samples and add them to the second 500 samples to get our 500 sample Time-Aliased-Hann result: 57 | 58 | ![Time-Aliased-Hann applied to input signal](time_aliased_hann_applied.jpg) 59 | 60 | # Pitch Synchronous Time-Aliased-Hann FFTs 61 | 62 | Highly harmonic signals like voiced speech have repeating wave forms that slowly evolve over time. By detecting two similar adjacent pitch periods, with an algorithm like AMDF, we can significantly reduce noise in the FFT. One property of Time-Aliased-Hann FFTs is that 100% of all harmonic energy goes to the correct FFT bin, with no leakage. This makes it especially good for analyzing speech, since voiced speech is highly harmonic. 63 | 64 | Here is a spectrogram of the phrase [With the pro golfer](voxin_golfer.wav), generated with the voxin TTS engine: 65 | 66 | ![With the pro golfer spectrogram](voxin_golfer.png) 67 | 68 | Here is the same sound analyzed using Praat, with a short-time FFT using Hamming windows. 69 | 70 | ![Praat spectrogram of same](praat_golfer.png) 71 | 72 | An important benefit of the pitch synchronous Time-Aliased-Hann technique is computational efficiency. The Praat spectrogram was made using 1000 500-sample FFTs. The Time-Aliased-Hann version was done with only 163 FFTs, of an average of 98 points each. Since frame size vary, and are short, result frames were scaled to 200 points using linear interpolation. This is roughly a 40X speed improvement, assuming FFTs are O(N*log2(N)). Also, the artifacts created by pitch periods entering and leaving the Hamming window are gone. Notice the vertical and horizontal lines in the spectrogram above. These are essentially noise that make it harder to match spectrograms for speech recognition. The better spectral resolution and low noise makes matching spectrograms a reasonable approach to speech recognition. 73 | 74 | # Alternative Time-Aliased Window Functions 75 | 76 | The Time-Aliased technique can be applied with any window function, not just Hann. However, I find window functions with the following property give better results: 77 | 78 | w(n) + w(n + N) = constant, n = 0 .. N - 1 79 | 80 | This is a bit more restrictive than the "weak COLA" constraint. This property insures that all pure harmonics of the fundamental pitch are exactly represented in the correct frequency bin with no leakage. Other window functions that have this property are Triangle and Blackman windows. Time-Aliased-Triangle has considerably more spectral noise, but still far less than traditional Triangle windows. Time-Aliased-Blackman windows have lower spectral leakage than Time-Aliased-Hann, with just a bit more complexity. 81 | 82 | The same voxin sample above generates this spectrogram with Time-Aliased-Blackman. It seems about the same to me. 83 | 84 | ![With the pro golfer spectrogram, using Time-Aliased-Blackman window](voxin_golfer_blackman.png) 85 | 86 | These three spectrograms are of a time-varying sine wave, showing spectral leakage for Time-Aliased-Triangle, Time-Aliased-Hann, and Time-Aliased-Blackman. They are generated with 100 point Time-Aliased-FFTs, and plotted with 70 DB dynamic range (0 is 70 DB below the peak value). As above, results are scaled to 200 points with linear interpolation. The frequency varies from 100 Hz to 4 KHz over a 2 second time frame. 87 | 88 | ![Time-Aliased-Triangle](sine_triangle.png "Time-Aliased-Triangle") 89 | 90 | Time-Aliased-Triangle 91 | 92 | ![Time-Aliased-Hann](sine_hann.png "Time-Aliased-Hann") 93 | 94 | Time-Aliased-Hann 95 | 96 | ![Time-Aliased-Blackman](sine_blackman.png "Time-Aliased-Blackman") 97 | 98 | Time-Aliased-Blackman 99 | 100 | Note that these signals are not harmonics of the frame rate. If they had been, there would be no leakage at all. 101 | 102 | For comparison, here's the same signal analyzed the same way, but with a traditional rectangle window in the first image, a traditional Hann window in the second, and a traditional Hamming window in the third: 103 | 104 | ![Rectangle window](rectangle.png "Rectangle window") 105 | 106 | Traditional rectangle window 107 | 108 | ![Hann window](hann.png "Hann window") 109 | 110 | Traditional Hann window 111 | 112 | ![Hamming window](hamming.png "Hamming window") 113 | 114 | Traditional Hamming window 115 | 116 | # Applications in Speech Synthesis and Recognition 117 | 118 | For both speech synthesis and recognition, common practice is to use LPC (Linear Predictive Coding) based analysis rather than directly matching FFTs. LPC is essentially a model order reduction method, and is lossy. Popular TTS engines based on the MBROLA algorithm family all sound "tinny", though I have come to think of this distortion as "LPC-ish". Speech recognition algorithms often reduce a spectrogram to 25 "Mel scale" frequency bins, losing much of the clarity of the signal. In both cases, computational efficiency is a key driver. With the improved clarity and computational efficiency of Time-Aliased-Hann FFTs, it should be possible to significantly reduce distortion in TTS engines, while improving accuracy in speech recognition engines. 119 | 120 | # Proof of Zero Leakage of Harmonics 121 | 122 | Let the input signal x be a sine wave defined as: 123 | 124 | x(n) = cos(2*pi*f*n/fs + p), n = 0 .. 2*N - 1, 125 | 126 | where f the frequency, fs is the sample rate, p is the phase, and and N is the window length. We're interested in harmonics, which occur at frequencies: 127 | 128 | f = m*fs/N, m an integer >= 1 129 | 130 | Rewriting x(n): 131 | 132 | x(n) = cos(2*pi*(m*fs/N)*n/fs + p) 133 | = cos(2*pi*m*n/N + p) 134 | 135 | The windowed frame, x'(n), is: 136 | 137 | w(n) = (1 - cos(pi*n/N))/2, n = 0 .. 2*N - 1. 138 | x'(n) = (1 - cos(pi*n/N))*x(n)/2 + (1 - cos(pi*(n + N)/N))*x(n + N)/2 139 | = (1 - cos(pi*n/N))*cos(2*pi*m*n/N + p)/2 + 140 | (1 - cos(pi*(n + N)/N))*cos(2*pi*m*(n + N)/N + p)/2 141 | = (cos(2*pi*m*n/N + p) + cos(2*pi*m*(n + N)/N + p))/2 142 | - (cos(pi*n/N)*cos(2*pi*m*n/N + p) + cos(pi*(n + N)/N)*cos(2*pi*m*(n + N)/N + p))/2 143 | = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2 144 | - (cos(pi*n/N)*cos(2*pi*m*n/N + p) + cos(pi*(n + N)/N)*cos(2*pi*m*(n + N)/N + p))/2 145 | 146 | Since cos(a)*cos(b) = cos(a + b)/2 + cos(a - b)/2, 147 | 148 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2 149 | - (cos(2*pi*m*n/N + p + pi*n/N) + cos(2*pi*m*n/N + p - pi*n/N) 150 | + cos(2*pi*m*(n + N)/N + p + pi*(n + N)/N) 151 | + cos(2*pi*m*(n + N)/N + p - pi*(n + N)/N))/4 152 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2 153 | - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p) 154 | + cos((n/N)*(2*pi*m + pi) + p + 2*pi*m + pi) 155 | + cos((n/N)*(2*pi*m - pi) + p + 2*pi*m - pi))/4 156 | 157 | Using cos(a +/- pi) = -cos(a): 158 | 159 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2 160 | - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p) 161 | - cos((n/N)*(2*pi*m + pi) + p + 2*pi*m) 162 | - cos((n/N)*(2*pi*m - pi) + p + 2*pi*m))/4 163 | 164 | Now, assuming m is an integer >=1, then cos(a + 2_pi_m) = cos(a): 165 | 166 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p))/2 167 | - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p) 168 | - cos((n/N)*(2*pi*m + pi) + p) 169 | - cos((n/N)*(2*pi*m - pi) + p))/4 170 | = cos(2*pi*m*n/N + p) 171 | = x(n) 172 | 173 | Thus, x'(n) = x(n), and as with a rectangle window, the FFT will have only one non-zero value, at m. So, harmonic energy does not leak at all. 174 | 175 | For anti-harmonics, at frequencies where m is a positive integer + 1/2: 176 | 177 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + pi))/2 178 | - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p) 179 | - cos((n/N)*(2*pi*m + pi) + p + pi) 180 | - cos((n/N)*(2*pi*m - pi) + p + pi))/4 181 | x'(n) = (cos(2*pi*m*n/N + p) - cos(2*pi*m*n/N + p))/2 182 | - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p) 183 | + cos((n/N)*(2*pi*m + pi) + p) 184 | + cos((n/N)*(2*pi*m - pi) + p))/4 185 | x'(n) = - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)/2 186 | 187 | This is 1/2 times a sine wave in the bin before f, plus 1/2 times a sine wave in the bin after f. Thus, anti-harmonics also have no leakage. 188 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 |

Pitch Synchronous Time-Aliased-Hann FFTs for Speech Recognition

2 |

By Bill Cox, waywardgeek@gmail.com

3 |

Intuitively, a Time-Aliased-Hann window is a sound sample that starts playing in the 4 | middle, and slowly fades to zero, while at the same time we start playing 5 | samples from beginning and slowly fade in. It is a waveform that smoothly feeds 6 | into itself. If the signal is a single pitch period of a harmonic signal, like a 7 | voice or a single note of an instrument, then this signal can be played over and 8 | over, and will sound like the note has been extended indefinitely, without 9 | noticeable distortion. This exact algorithm is commonly used in time-domain 10 | techniques used in the TD-PSOLA class of algorithms. I wrote a Linux package called 11 | "sonic", which is used for speeding up speech, and which is particularly good at 12 | low distortion for speeds of 2X and up. It's basically 13 | PICOLA, but with 14 | a new algorithm that kicks in at 2X and above. Such an algorithm is needed for 15 | improving existing text-to-speech engines to provide very high speed speech for 16 | blind speed listeners. After trying various FFT and TD-PSOLA based methods, I 17 | found that PICOLA produces far less distortion in sped up speech, even compared 18 | to the popular WSOLA algorithm, and so I investigated why that is so. The 19 | answer seems to be that the pitch synchronous overlap-add step produces no 20 | spectral distortion of harmonics, and little distortion of everything else. So, 21 | I felt it might work well as a pre-process to an FFT, especially if the FFT 22 | frames are single pitch periods. I was amazed with the results.

23 |

DSP algorithms developers often use window 24 | functions such as the popular Hamming 25 | window to reduce spectral leakage in their FFTs.

26 |

So far as I know, none of this is new, with the possible exception of applying 27 | Time-Aliased-Hann pitch synchronously to speech. A response on comp.dsp was so 28 | informative, I think its worth reposting as-is here:

29 |
30 |

It's always nice to see someone new rediscover the advantages of the 31 | independence of window size from DFT size. The approach is not new. It 32 | can be found in a number of guises.

33 |

It has long been used as one form of FFT based polyphase filter. For 34 | examples: "polyphase DFT filterbank", 35 | "PolyDFT" and "PFT" as used in places such as: 36 | http://www.rfel.com/download/W03006-Comparison_of_FFT_and_PolyDFT_Transient= 37 | _Response.pdf

38 |

Fred Harris presents the concept in the 'Time Domain Signal Processing 39 | with the DFT' chapter in Doug Elliot=92s 1987 book Handbook of Digital 40 | Signal Processing. He considers the name "data-wrapping" but considers 41 | that too close to the context of "wrapping" in "phase unwrapping" and 42 | suggests "data folding" instead.

43 |

A couple more terms approaching yours are "weighted overlap-add 44 | structure" or "windowed-presum FFT" from "Understanding Digital Signal 45 | Processing, Second Edition" by Richard G. Lyons as explained in: 46 | http://www.eetimes.com/design/embedded/4007611/DSP-Tricks-Building-a-practi= 47 | cal-spectrum-analyzer

48 |

The most complete exposition on it that I have found comes in a 300+ 49 | page opus: 50 | TIME ALIASING METHODS OF SPECTRUM ESTIMATION 51 | by Jason F Dahl 52 | A dissertation submitted to the faculty of Brigham Young University, 53 | Dept. of Mechanical Engineering, April 2003. 305pp 54 | Available at: 55 | http://contentdm.lib.byu.edu/ETD/image/etd157.pdf

56 |

I personally prefer Dahl's "time aliasing" as having the least 57 | conflict with other uses of the component words.

58 |

This literature provides comparisons to other windows and window 59 | design methods.

60 |

Dale B. Dalrymple

61 |
62 |

Having now read these papers, and Jason Dahl's dissertation, I feel I have a much better understanding of time-aliased techniques and their applications. The pitch synchronous time-aliased FFT with a low leakage window like a Hanning window is excellent for speech analysis.

63 |

Definition of Time-Aliased-Hann

64 |

Applying a Time-Aliased-Hann window is done by first applying a Hann window to two 65 | adjacent FFT input frames, and then adding the first frame to the second. This 66 | second step is described in various "time aliasing" techniques for DFTs, and in 67 | general is not restricted to two frames, but can be any number of input frames. 68 | However, for pitch synchronous speech processing, exactly two pitch periods 69 | works quite well. More frames make the time resolution too poor. Given two 70 | frames worth of input samples, x(n), n = 0 .. 2*N - 71 | 1, we compute the windowed samples x'(n), n = 0 .. N - 1:

72 |
w(n) = (1 - cos(pi*n/N))/2, n = 0 .. 2*N - 1.
 73 | x'(n) = w(n)*x(n) + w(n + N)*x(n + N), n = 0 .. N - 1
 74 | 
75 |

Here is a plot of worst-case spectral noise for Hamming windows vs Time-Aliased-Hann 76 | windows, at about 1KHz:

77 |

Hamming vs Time-Aliased-Hann

78 |

This plot was created using time_aliased_hann.py and 79 | hamming_py. This plot basically shows that we should not use 80 | Hamming windows for speech spectrograms, as is commonly done. The spectral 81 | noise is too high for high dynamic range spectrograms like these. Hann 82 | windows work better. The Time-Aliased-Hann window is exactly the same as a 83 | normal FFT response of a Hann-windowed sample of 2 pitch periods, but we drop 84 | every other frequency bin, keeping only harmonics of the fundamental pitch.

85 |

What it Looks Like

86 |

Let's see an example. In this case, we're using a frame size of 500. 87 | The input signal is a worst case for Time-Aliased-Hann: 5.75 88 | sine wave cycles per frame, neither harmonic, or anti-harmonic, but exactly in between. 89 | Here's two frames worth of input data:

90 |

Input signal

91 |

We apply a Hann window function to two frames, or 1000 samples. Here's what a 92 | Hann function looks like:

93 |

Hann window

94 |

We multiply the Hann function onto the input signal to get:

95 |

Hann window applied to input signal

96 |

Finally, we take the first 500 samples and add them to the second 500 samples to 97 | get our 500 sample Time-Aliased-Hann result:

98 |

Time-Aliased-Hann applied to input signal

99 |

Pitch Synchronous Time-Aliased-Hann FFTs

100 |

Highly harmonic signals like voiced speech have repeating wave forms that slowly 101 | evolve over time. By detecting two similar adjacent pitch periods, with an 102 | algorithm like AMDF, we can significantly reduce noise in the FFT. One property 103 | of Time-Aliased-Hann FFTs is that 100% of all harmonic energy goes to the correct FFT 104 | bin, with no leakage. This makes it especially good for analyzing speech, since 105 | voiced speech is highly harmonic.

106 |

Here is a spectrogram of the phrase With the pro golfer, 107 | generated with the voxin TTS engine:

108 |

With the pro golfer spectrogram

109 |

Here is the same sound analyzed using Praat, with a short-time FFT using Hamming 110 | windows.

111 |

Praat spectrogram of same

112 |

An important benefit of the pitch synchronous Time-Aliased-Hann technique is 113 | computational efficiency. The Praat spectrogram was made using 1000 500-sample 114 | FFTs. The Time-Aliased-Hann version was done with only 163 FFTs, of an average of 98 115 | points each. Since frame size vary, and are short, result frames were scaled to 116 | 200 points using linear interpolation. This is roughly a 40X speed improvement, 117 | assuming FFTs are O(N*log2(N)). Also, the artifacts created by pitch periods 118 | entering and leaving the Hamming window are gone. Notice the vertical and 119 | horizontal lines in the spectrogram above. These are essentially noise that 120 | make it harder to match spectrograms for speech recognition. The better spectral 121 | resolution and low noise makes matching spectrograms a reasonable approach to speech 122 | recognition.

123 |

Alternative Time-Aliased Window Functions

124 |

The Time-Aliased technique can be applied with any window function, not just Hann. 125 | However, I find window functions with the following property give better results:

126 |
w(n) + w(n + N) = constant, n = 0 .. N - 1
127 | 
128 |

This is a bit more restrictive than the "weak COLA" constraint. This property 129 | insures that all pure harmonics of the fundamental pitch are exactly represented 130 | in the correct frequency bin with no leakage. Other window functions that have 131 | this property are Triangle and Blackman windows. Time-Aliased-Triangle has 132 | considerably more spectral noise, but still far less than traditional Triangle 133 | windows. Time-Aliased-Blackman windows have lower spectral leakage than 134 | Time-Aliased-Hann, with just a bit more complexity.

135 |

The same voxin sample above generates this spectrogram with Time-Aliased-Blackman. It 136 | seems about the same to me.

137 |

With the pro golfer spectrogram, using Time-Aliased-Blackman window

138 |

These three spectrograms are of a time-varying sine wave, showing spectral 139 | leakage for Time-Aliased-Triangle, Time-Aliased-Hann, and Time-Aliased-Blackman. 140 | They are generated with 100 point Time-Aliased-FFTs, and plotted with 70 DB 141 | dynamic range (0 is 70 DB below the peak value). As above, results are scaled 142 | to 200 points with linear interpolation. The frequency varies from 100 Hz to 4 143 | KHz over a 2 second time frame.

144 |

Time-Aliased-Triangle

145 |

Time-Aliased-Triangle

146 |

Time-Aliased-Hann

147 |

Time-Aliased-Hann

148 |

Time-Aliased-Blackman

149 |

Time-Aliased-Blackman

150 |

Note that these signals are not harmonics of the frame rate. If they had been, there would be no leakage at all.

151 |

For comparison, here's the same signal analyzed the same way, but with a 152 | traditional rectangle window in the first image, a traditional Hann window 153 | in the second, and a traditional Hamming window in the third:

154 |

Rectangle window

155 |

Traditional rectangle window

156 |

Hann window

157 |

Traditional Hann window

158 |

Hamming window

159 |

Traditional Hamming window

160 |

Applications in Speech Synthesis and Recognition

161 |

For both speech synthesis and recognition, common practice is to use LPC (Linear 162 | Predictive Coding) based analysis rather than directly matching FFTs. LPC is 163 | essentially a model order reduction method, and is lossy. Popular TTS engines 164 | based on the MBROLA algorithm family all sound "tinny", though I have come to 165 | think of this distortion as "LPC-ish". Speech recognition algorithms often 166 | reduce a spectrogram to 25 "Mel scale" frequency bins, losing much of the 167 | clarity of the signal. In both cases, computational efficiency is a key driver. 168 | With the improved clarity and computational efficiency of Time-Aliased-Hann FFTs, it 169 | should be possible to significantly reduce distortion in TTS engines, while 170 | improving accuracy in speech recognition engines.

171 |

Proof of Zero Leakage of Harmonics

172 |

Let the input signal x be a sine wave defined as:

173 |
x(n) = cos(2*pi*f*n/fs + p), n = 0 .. 2*N - 1,
174 | 
175 |

where f the frequency, fs is the sample rate, p is the phase, and and N is 176 | the window length. We're interested in harmonics, which occur at frequencies:

177 |
f = m*fs/N, m an integer >= 1
178 | 
179 |

Rewriting x(n):

180 |
x(n) = cos(2*pi*(m*fs/N)*n/fs + p)
181 |     = cos(2*pi*m*n/N + p)
182 | 
183 |

The windowed frame, x'(n), is:

184 |
w(n) = (1 - cos(pi*n/N))/2, n = 0 .. 2*N - 1.
185 | x'(n) = (1 - cos(pi*n/N))*x(n)/2 + (1 - cos(pi*(n + N)/N))*x(n + N)/2
186 |     = (1 - cos(pi*n/N))*cos(2*pi*m*n/N + p)/2 +
187 |     (1 - cos(pi*(n + N)/N))*cos(2*pi*m*(n + N)/N + p)/2
188 | = (cos(2*pi*m*n/N + p) + cos(2*pi*m*(n + N)/N + p))/2
189 |   - (cos(pi*n/N)*cos(2*pi*m*n/N + p) + cos(pi*(n + N)/N)*cos(2*pi*m*(n + N)/N + p))/2
190 | = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2
191 |   - (cos(pi*n/N)*cos(2*pi*m*n/N + p) + cos(pi*(n + N)/N)*cos(2*pi*m*(n + N)/N + p))/2
192 | 
193 |

Since cos(a)*cos(b) = cos(a + b)/2 + cos(a - b)/2,

194 |
x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2
195 |   - (cos(2*pi*m*n/N + p + pi*n/N) + cos(2*pi*m*n/N + p - pi*n/N)
196 |       + cos(2*pi*m*(n + N)/N + p + pi*(n + N)/N)
197 |       + cos(2*pi*m*(n + N)/N + p - pi*(n + N)/N))/4
198 | x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2
199 |   - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)
200 |       + cos((n/N)*(2*pi*m + pi) + p + 2*pi*m + pi)
201 |       + cos((n/N)*(2*pi*m - pi) + p + 2*pi*m - pi))/4
202 | 
203 |

Using cos(a +/- pi) = -cos(a):

204 |
x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + 2*pi*m))/2
205 |   - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)
206 |       - cos((n/N)*(2*pi*m + pi) + p + 2*pi*m)
207 |       - cos((n/N)*(2*pi*m - pi) + p + 2*pi*m))/4
208 | 
209 |

Now, assuming m is an integer >=1, then cos(a + 2pim) = cos(a):

210 |
x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p))/2
211 |   - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)
212 |       - cos((n/N)*(2*pi*m + pi) + p)
213 |       - cos((n/N)*(2*pi*m - pi) + p))/4
214 |     = cos(2*pi*m*n/N + p)
215 | = x(n)
216 | 
217 |

Thus, x'(n) = x(n), and as with a rectangle window, the FFT will have only one 218 | non-zero value, at m. So, harmonic energy does not leak at all.

219 |

For anti-harmonics, at frequencies where m is a positive integer + 1/2:

220 |
x'(n) = (cos(2*pi*m*n/N + p) + cos(2*pi*m*n/N + p + pi))/2
221 |   - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)
222 |       - cos((n/N)*(2*pi*m + pi) + p + pi)
223 |       - cos((n/N)*(2*pi*m - pi) + p + pi))/4
224 | x'(n) = (cos(2*pi*m*n/N + p) - cos(2*pi*m*n/N + p))/2
225 |   - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)
226 |       + cos((n/N)*(2*pi*m + pi) + p)
227 |       + cos((n/N)*(2*pi*m - pi) + p))/4
228 | x'(n) = - (cos((n/N)*(2*pi*m + pi) + p) + cos((n/N)*(2*pi*m - pi) + p)/2
229 | 
230 |

This is 1/2 times a sine wave in the bin before f, plus 1/2 times a sine wave in 231 | the bin after f. Thus, anti-harmonics also have no leakage.

--------------------------------------------------------------------------------