torchaudio.transforms.Spectrogram

username: Elahe_Rahimian
cooked: <p>Hi Everybody,</p>
<p>I am using the torchaudio.transforms.Spectrogram to get the Spectrogram of a sin wave which is as follows:<br>
Fs = 400<br>
freq = 5<br>
sample = 400<br>
x = np.arange(sample)<br>
y = np.sin(2 * np.pi * freq * x / Fs)</p>
<p>Then, I get the Spectrogram of the mentioned sin wave as follows:<br>
specgram = torchaudio.transforms.Spectrogram(n_fft=256,<br>
win_length=256,<br>
hop_length=184,<br>
window_fn=torch.hamming_window,<br>
power=1,<br>
normalized=True)</p>
<p>output = specgram(torch.from_numpy(y))<br>
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:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2.png" data-download-href="https://discuss.pytorch.org/uploads/default/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2" title="specg"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2_2_690x295.png" alt="specg" data-base62-sha1="aK8BJuXvecZP9fRilfAB3ofAARY" width="690" height="295" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2_2_690x295.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2_2_1035x442.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b4ceb478bb352bd217683eaa5ad2a8ccdad42b2_2_1380x590.png 2x" data-dominant-color="4F1660"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">specg</span><span class="informations">1514×649 8.23 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I believe this is wrong, so I decided to used the signal library to get Spectrogram of the mention sin wave as follows:<br>
frequencies_samples, time_segment_sample, spectrogram_of_vector = signal.spectrogram(<br>
x=y,<br>
fs=fs,<br>
nperseg=256,<br>
noverlap=184,<br>
window=“hamming”,<br>
detrend=False,<br>
mode=‘magnitude’)<br>
And, then by using  signal.spectrogram, I get the following figure:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/4/d476e368d3ebfb8672617f5d01d83bfc5686277b.png" data-download-href="https://discuss.pytorch.org/uploads/default/d476e368d3ebfb8672617f5d01d83bfc5686277b" title="spec2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/4/d476e368d3ebfb8672617f5d01d83bfc5686277b_2_690x298.png" alt="spec2" data-base62-sha1="ujxWkmMV49BHx2jeeXVW4D9C3J9" width="690" height="298" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/4/d476e368d3ebfb8672617f5d01d83bfc5686277b_2_690x298.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/4/d476e368d3ebfb8672617f5d01d83bfc5686277b_2_1035x447.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/4/d476e368d3ebfb8672617f5d01d83bfc5686277b_2_1380x596.png 2x" data-dominant-color="4F125D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">spec2</span><span class="informations">1498×649 4.26 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>As we expected, the spectrogram should not change in different time bins because we have only one frequency.</p>
<p>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?</p>

username: tom
cooked: <p>This probably is from the phase shift incurred when the partitioning isn’t a multiple of the wave length in samples.</p>

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

username: tom
cooked: <p>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.</p>

username: Elahe_Rahimian
cooked: <p>Many thanks for your reply.<br>
I am just wondering why we do not see this effect when we get the spectrogram by Signal library (signal.spectrogram).</p>