Friday, September 5, 2025
HomeLanguagesnumpy bartlett() in Python

numpy bartlett() in Python

The Bartlett window is very similar to a triangular window, except that the endpoints are at zero. It is often used in signal processing for tapering a signal, without generating too much ripple in the frequency domain.

Parameters(numpy.bartlett(M)): 
M : int Number of points in the output window. 
     If zero or less, an empty array is returned.

Returns: 
out : array

The triangular window, with the maximum value normalized to one (the value one appears only if the number of samples is odd), with the first and last samples equal to zero.

Example:




import numpy as np
print(np.bartlett(12))


Output:

[ 0.          0.18181818  0.36363636  0.54545455  0.72727273  0.90909091
  0.90909091  0.72727273  0.54545455  0.36363636  0.18181818  0.        ]

Plotting the window and its frequency response (requires SciPy and matplotlib):
For Window:




import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft, fftshift
window = np.bartlett(51)
plt.plot(window)
plt.title("Bartlett window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()


Output:

For frequency:




import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft, fftshift
window = np.bartlett(51)
plt.figure()
A = fft(window, 2048) / 25.5
mag = np.abs(fftshift(A))
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(mag)
response = np.clip(response, -100, 100)
plt.plot(freq, response)
plt.title("Frequency response of Bartlett window")
plt.ylabel("Magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.axis('tight')
plt.show()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS