'Which parameters of welch do determin the length of the output? (python)

I am using welch in python. Which parameter in welch does define the length of the output array? Based on my trials, the output length is related to nperseg/2; but I cannot understand its reason and mathematics. And, I am not sure about the effect of other parameters on the output length.

Also, there is not enough explanation in its documentation (https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.welch.html)

I will be more than happy if anyone can help me. I could not find any clear info on the web!



Solution 1:[1]

Parsing the sentence from the documents

Welch’s method [1] computes an estimate of the power spectral density by dividing the data into overlapping segments, computing a modified periodogram for each segment and averaging the periodograms.

def periodogram(x, Ts=1.0):
  '''
    This function will compute one score for each period 
    in 
  '''
  return abs(np.fft.rfft(x))**2

Then they say that the data is divided in overlapped segments and then added, so it is something like this

def welsh(data, nperseg, noverlap, window):
  stride = nperseg - noverlap
  return sum(periodogram(data[i*stride:i*stride+nperseg])
    for i in range((len(data) - nperseg) // stride))

i.e. you take segments of length nperseg to compute the periodograms, since you can get the periodogram only up to the Nyquist frequency, you end up having nperseg/2 (for nperseg).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bob