Hi Everybody,

I am using the torchaudio.transforms.Spectrogram to get the Spectrogram of a sin wave which is as follows:
Fs = 400
freq = 5
sample = 400
x = np.arange(sample)
y = np.sin(2 * np.pi * freq * x / Fs)

Then, I get the Spectrogram of the mentioned sin wave as follows:
specgram = torchaudio.transforms.Spectrogram(n_fft=256,

output = specgram(torch.from_numpy(y))
As you see, the sin wave only has a frequency 5 Hz, so I expect the output does not change in different time bins, but I got the following figure which is so strange:

I believe this is wrong, so I decided to used the signal library to get Spectrogram of the mention sin wave as follows:
frequencies_samples, time_segment_sample, spectrogram_of_vector = signal.spectrogram(
And, then by using signal.spectrogram, I get the following figure:

As we expected, the spectrogram should not change in different time bins because we have only one frequency.

So, what is the matter with the spectrogram when I use torch library? why it changes over the time when I have only one frequency?

This probably is from the phase shift incurred when the partitioning isn’t a multiple of the wave length in samples.

Many thanks for your reply,
I applied a window which is a multiple of the wave length in samples, and now it is as I expected.
However, I have another question.
The output of torchaudio.transforms.Spectrogram is Exponent for the magnitude spectrogram. So, why we can see the effect of phase shift?

I think the phase changes the jump at the discontinuity at the boundary of basic cell and that leads to varying degrees of uncleanliness of the Fourier transform. If you had a periodic thing, the phase shift would correspond to an argument shift of the (complex) coefficients but leaving the modules of the coefficients alone.

Many thanks for your reply.
I am just wondering why we do not see this effect when we get the spectrogram by Signal library (signal.spectrogram).