# Why does frequency spectrum gets flipped in STFT compared to the one generated from FFT?

19 vues (au cours des 30 derniers jours)
Edward le 30 Août 2023
Modifié(e) : Edward le 1 Sep 2023
I picked out a signal of 200 temporal samples and processed it separately with STFT and FFT. The data is attached as .mat file.
The code for STFT is listed below (the first dimension of Matrix has 200 temporal samples and I plot the spectrogram of first signal segment):
[s,f,t] = stft(Matrix',5000/3,Window=rectwin(32),OverlapLength=ceil(0.8*32),FFTLength=200); % Full map
s_amp = abs(s);
figure;plot(squeeze(s_amp(:,1,1)));
The code for FFT is shown here (I get the first 32 temporal samples and do the FFT, then the spectrum is zero centered):
FreqSpec_temp = fft(squeeze(Matrix(1,1:32)),[]);
FreqSpec_shift = fftshift(FreqSpec_temp);
figure;plot(abs(FreqSpec_shift));
The plots of STFT (left) and FFT (right) are displayed. One can easily spot out that the frequency spectrum of STFT is horizontally flipped compared to the spectrum of FFT. Why is this the case? Which method should I use to get an accurate frequency spectrum? I will need to calculate the mean frequency of the signal based on the spectrum.
Besides, how could the spectrum from STFT has 200 points? How does it work out the interpolation? Thank you for your help!  ##### 2 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Edward le 31 Août 2023
Hi Paul,
For FFT, when it takes in 32 temporal samples, the frequency spectrum will also have 32 points for representing the spectrum. But for STFT, it takes in 32 temporal samples for processing 1 temporal segment, while the frequency spectrum has 200 points. What I wonder was certain interpolation was done in generating the frequency spectrum, but the process was not specified in the page of STFT. I would like to know what kind of interpolation was used to get 200 points from an input of 32 points.

Connectez-vous pour commenter.

### Réponse acceptée

Paul le 31 Août 2023
Using random data
rng(100);
Matrix = rand(1000,1);
Compute the stft
[s,f,t] = stft(Matrix,5000/3,Window=rectwin(32),OverlapLength=ceil(0.8*32),FFTLength=200); % Full map
s_amp = abs(s);
plot amplitude vs frequency
figure;plot(f,squeeze(s_amp(:,1,1,1)));
Compute the fft, zero pad to 200 to match FFTLength
FreqSpec_temp = fft(squeeze(Matrix(1:32,1,1)),200);
FreqSpec_shift = fftshift(FreqSpec_temp);
Plot amplitude vs frequency, matches first column of stft
hold on
plot((-100:99)/200*5000/3,abs(FreqSpec_shift),'o'); ##### 3 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Edward le 1 Sep 2023
Modifié(e) : Edward le 1 Sep 2023
Thank you, Paul! Yes, you're right. I wanted to use transpose (.') only, but I didn't realize ' actually represents as conjugate transpose. It was my coding mistake. And thank you for explaining the difference between the 2 curves!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Multirate Signal Processing dans Help Center et File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!