Skip to content

Instantly share code, notes, and snippets.

@denis-bz
Last active October 28, 2025 11:58
Show Gist options
  • Select an option

  • Save denis-bz/658965a95980f496f554bdce1fe876b3 to your computer and use it in GitHub Desktop.

Select an option

Save denis-bz/658965a95980f496f554bdce1fe876b3 to your computer and use it in GitHub Desktop.
Fourier upsamplng a spike: why is odd n so big ? 28 Oct 2025

Fourier upsamplng a spike: why is odd n so big ?

Keywords: resample upsample interpolate frequency-domain Fourier scipy

The problem:

28oct-spike-upsample
from scipy.signal import resample  # in Fourier space

for n in [100, 101]:
	x = spike( n )  # [100 0 0 ...]
	y = resample( x, 2*n )  # interpolate / upsample
	print the middle part 
	plot

n 100  y middle * 100: [ -8  0 5   0 -2   0  -2   0 5  0 -8]
n 101  y middle * 100: [  0 99 0 -99  0  99   0 -99 0 99  0]

spike is an extreme or silly testcase, but why is odd n so big ?

resample does: rfft( x ), 0-pad that at the end (inside irfft), irfft.

Links

resample doc
resample .py
wikipedia Upsampling interleaves 0s, [x0 0 x1 0 ...] in the time domain
dsp.stack why-to-pad-zeros-at-the-middle ff.

cheers
— denis 2025-10-28 October

#!/usr/bin/env python3
r"""Spike [100 0 0 ...] $\ \to \ $ resample $ %d \times nx $
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.html
"""
# keywords: resample upsample interpolate frequency-domain Fourier scipy
# https://github.com/scipy/scipy/blob/main/scipy/signal/_signaltools.py
# $scipy/signal/_signaltools.py resample
# $scipy/fft/_basic.py irfft
# https://en.wikipedia.org/wiki/Upsampling#Upsampling_by_an_integer_factor
# *separated by* L − 1 zeros
import numpy as np
from scipy.signal import resample
import matplotlib.pyplot as pl
import sys
print( 80 * "▄" )
np.set_printoptions( edgeitems=10, threshold=10, linewidth=120,
formatter = dict( float = lambda x: "%.2g" % x )) # 2 digits
def spike( n ):
x = np.zeros( n )
x[0] = 100
return x
ns = [100, 101]
zoom = 2
plot = 0
# to change these params, run this.py a=1 b=2,None 'c = expr' ... in sh or ipython --
for arg in sys.argv[1:]:
exec( arg )
title = __doc__.strip() % zoom
print( title, "\n" )
if plot:
fig, axes = pl.subplots( nrows=len(ns), figsize=[8, 5] )
fig.suptitle( title, multialignment="left" )
else:
axes = len(ns) * [None]
for n, ax in zip( ns, axes ):
x = spike( n )
print( "\n" + 80 * "-" )
nout = zoom * n
#...............................................................................
y = resample( x, nout )
jmid = nout // 2
ymid = (y[jmid-5 : jmid+5+1] * 100) .round().astype(int)
print( "n %d y middle * 100: %s " % ( n, ymid ))
if plot:
ax.set_title( f"n = {n}", loc="center", y=0.9 )
ax.set( ylim=[-2, 2] )
ax.plot( y, "s-", lw=0.36, ms=1, color="crimson" )
if plot >= 2:
import zz
zz.savefig( __file__, info=__file__ ) # sign, date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment