Zero padding - before or after the windowing?











up vote
0
down vote

favorite












I am trying to perform the STFT on a speech signal. I'd like to know when I should do the zero-padding, before windowing each frame, or after?



My attempt:



x    = signal(:);                   % make sure signal is a column vector
sL = length(x); % signal length
wL = fix(window_Time*fs); % window length
HOP = floor(wL*(1 - overlap/100)); % windowing step
win = gausswin(wL); % window type
F = 1 + fix((sL-wL)/HOP); % calculate the number of signal frames
stft = zeros(nfft,F); % preallocate stft matrix, [nfftxF]

for f = 0:F-1
xw = x(1+f*HOP:wL+f*HOP).*win; % windowing
X = fftshift(fft([xw;zeros(wL,1)],nfft)); % FFT shifted (double-sided)
stft(:,1+f) = X; % update of the stft matrix
end









share|improve this question
























  • Not a programming question - try dsp.stackexchange.com ?
    – Paul R
    Nov 11 at 18:07










  • @PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
    – user10516309
    Nov 11 at 18:40















up vote
0
down vote

favorite












I am trying to perform the STFT on a speech signal. I'd like to know when I should do the zero-padding, before windowing each frame, or after?



My attempt:



x    = signal(:);                   % make sure signal is a column vector
sL = length(x); % signal length
wL = fix(window_Time*fs); % window length
HOP = floor(wL*(1 - overlap/100)); % windowing step
win = gausswin(wL); % window type
F = 1 + fix((sL-wL)/HOP); % calculate the number of signal frames
stft = zeros(nfft,F); % preallocate stft matrix, [nfftxF]

for f = 0:F-1
xw = x(1+f*HOP:wL+f*HOP).*win; % windowing
X = fftshift(fft([xw;zeros(wL,1)],nfft)); % FFT shifted (double-sided)
stft(:,1+f) = X; % update of the stft matrix
end









share|improve this question
























  • Not a programming question - try dsp.stackexchange.com ?
    – Paul R
    Nov 11 at 18:07










  • @PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
    – user10516309
    Nov 11 at 18:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to perform the STFT on a speech signal. I'd like to know when I should do the zero-padding, before windowing each frame, or after?



My attempt:



x    = signal(:);                   % make sure signal is a column vector
sL = length(x); % signal length
wL = fix(window_Time*fs); % window length
HOP = floor(wL*(1 - overlap/100)); % windowing step
win = gausswin(wL); % window type
F = 1 + fix((sL-wL)/HOP); % calculate the number of signal frames
stft = zeros(nfft,F); % preallocate stft matrix, [nfftxF]

for f = 0:F-1
xw = x(1+f*HOP:wL+f*HOP).*win; % windowing
X = fftshift(fft([xw;zeros(wL,1)],nfft)); % FFT shifted (double-sided)
stft(:,1+f) = X; % update of the stft matrix
end









share|improve this question















I am trying to perform the STFT on a speech signal. I'd like to know when I should do the zero-padding, before windowing each frame, or after?



My attempt:



x    = signal(:);                   % make sure signal is a column vector
sL = length(x); % signal length
wL = fix(window_Time*fs); % window length
HOP = floor(wL*(1 - overlap/100)); % windowing step
win = gausswin(wL); % window type
F = 1 + fix((sL-wL)/HOP); % calculate the number of signal frames
stft = zeros(nfft,F); % preallocate stft matrix, [nfftxF]

for f = 0:F-1
xw = x(1+f*HOP:wL+f*HOP).*win; % windowing
X = fftshift(fft([xw;zeros(wL,1)],nfft)); % FFT shifted (double-sided)
stft(:,1+f) = X; % update of the stft matrix
end






signal-processing fft speech






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:48

























asked Nov 11 at 16:24







user10516309



















  • Not a programming question - try dsp.stackexchange.com ?
    – Paul R
    Nov 11 at 18:07










  • @PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
    – user10516309
    Nov 11 at 18:40


















  • Not a programming question - try dsp.stackexchange.com ?
    – Paul R
    Nov 11 at 18:07










  • @PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
    – user10516309
    Nov 11 at 18:40
















Not a programming question - try dsp.stackexchange.com ?
– Paul R
Nov 11 at 18:07




Not a programming question - try dsp.stackexchange.com ?
– Paul R
Nov 11 at 18:07












@PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
– user10516309
Nov 11 at 18:40




@PaulR I did not know about dsp.stackexchange.com. Thank you for the insight, I'll try there.
– user10516309
Nov 11 at 18:40












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You must do it before the FFT.




  1. Divide the signal into frames, see butter function;

  2. Apply a window to each frames: frames x window;

  3. Zero-padd the signal;

  4. FFT each windowed frames.


P.S. I've read somewhere that the FFT algorithm already does the zero-padding automatically, i.e., When you to the FFT(x) it will already “padd” the signal x with zeros at the end, to reach the length of the FFT.






share|improve this answer























  • Ok. That makes sense. Thank you @Dirac.
    – user10516309
    Nov 12 at 2:10




















up vote
-1
down vote













You zero pad frames inside window after windowing to make the window length the power of 2






share|improve this answer

















  • 1




    Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
    – user10516309
    Nov 11 at 18:37










  • Could you explain a little bit better the part of making the window length the power of 2? Why?
    – user10516309
    Nov 11 at 18:50








  • 1




    FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
    – Nikolay Shmyrev
    Nov 11 at 21:09






  • 1




    That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
    – user10516309
    Nov 11 at 23:07










  • I did not know that MATLAB does zero-pads automatically, but it makes sense.
    – user4061624
    Nov 12 at 1:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250745%2fzero-padding-before-or-after-the-windowing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You must do it before the FFT.




  1. Divide the signal into frames, see butter function;

  2. Apply a window to each frames: frames x window;

  3. Zero-padd the signal;

  4. FFT each windowed frames.


P.S. I've read somewhere that the FFT algorithm already does the zero-padding automatically, i.e., When you to the FFT(x) it will already “padd” the signal x with zeros at the end, to reach the length of the FFT.






share|improve this answer























  • Ok. That makes sense. Thank you @Dirac.
    – user10516309
    Nov 12 at 2:10

















up vote
0
down vote



accepted










You must do it before the FFT.




  1. Divide the signal into frames, see butter function;

  2. Apply a window to each frames: frames x window;

  3. Zero-padd the signal;

  4. FFT each windowed frames.


P.S. I've read somewhere that the FFT algorithm already does the zero-padding automatically, i.e., When you to the FFT(x) it will already “padd” the signal x with zeros at the end, to reach the length of the FFT.






share|improve this answer























  • Ok. That makes sense. Thank you @Dirac.
    – user10516309
    Nov 12 at 2:10















up vote
0
down vote



accepted







up vote
0
down vote



accepted






You must do it before the FFT.




  1. Divide the signal into frames, see butter function;

  2. Apply a window to each frames: frames x window;

  3. Zero-padd the signal;

  4. FFT each windowed frames.


P.S. I've read somewhere that the FFT algorithm already does the zero-padding automatically, i.e., When you to the FFT(x) it will already “padd” the signal x with zeros at the end, to reach the length of the FFT.






share|improve this answer














You must do it before the FFT.




  1. Divide the signal into frames, see butter function;

  2. Apply a window to each frames: frames x window;

  3. Zero-padd the signal;

  4. FFT each windowed frames.


P.S. I've read somewhere that the FFT algorithm already does the zero-padding automatically, i.e., When you to the FFT(x) it will already “padd” the signal x with zeros at the end, to reach the length of the FFT.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 14:44

























answered Nov 12 at 1:21







user4061624



















  • Ok. That makes sense. Thank you @Dirac.
    – user10516309
    Nov 12 at 2:10




















  • Ok. That makes sense. Thank you @Dirac.
    – user10516309
    Nov 12 at 2:10


















Ok. That makes sense. Thank you @Dirac.
– user10516309
Nov 12 at 2:10






Ok. That makes sense. Thank you @Dirac.
– user10516309
Nov 12 at 2:10














up vote
-1
down vote













You zero pad frames inside window after windowing to make the window length the power of 2






share|improve this answer

















  • 1




    Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
    – user10516309
    Nov 11 at 18:37










  • Could you explain a little bit better the part of making the window length the power of 2? Why?
    – user10516309
    Nov 11 at 18:50








  • 1




    FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
    – Nikolay Shmyrev
    Nov 11 at 21:09






  • 1




    That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
    – user10516309
    Nov 11 at 23:07










  • I did not know that MATLAB does zero-pads automatically, but it makes sense.
    – user4061624
    Nov 12 at 1:34















up vote
-1
down vote













You zero pad frames inside window after windowing to make the window length the power of 2






share|improve this answer

















  • 1




    Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
    – user10516309
    Nov 11 at 18:37










  • Could you explain a little bit better the part of making the window length the power of 2? Why?
    – user10516309
    Nov 11 at 18:50








  • 1




    FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
    – Nikolay Shmyrev
    Nov 11 at 21:09






  • 1




    That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
    – user10516309
    Nov 11 at 23:07










  • I did not know that MATLAB does zero-pads automatically, but it makes sense.
    – user4061624
    Nov 12 at 1:34













up vote
-1
down vote










up vote
-1
down vote









You zero pad frames inside window after windowing to make the window length the power of 2






share|improve this answer












You zero pad frames inside window after windowing to make the window length the power of 2







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:08









Nikolay Shmyrev

19.9k43063




19.9k43063








  • 1




    Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
    – user10516309
    Nov 11 at 18:37










  • Could you explain a little bit better the part of making the window length the power of 2? Why?
    – user10516309
    Nov 11 at 18:50








  • 1




    FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
    – Nikolay Shmyrev
    Nov 11 at 21:09






  • 1




    That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
    – user10516309
    Nov 11 at 23:07










  • I did not know that MATLAB does zero-pads automatically, but it makes sense.
    – user4061624
    Nov 12 at 1:34














  • 1




    Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
    – user10516309
    Nov 11 at 18:37










  • Could you explain a little bit better the part of making the window length the power of 2? Why?
    – user10516309
    Nov 11 at 18:50








  • 1




    FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
    – Nikolay Shmyrev
    Nov 11 at 21:09






  • 1




    That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
    – user10516309
    Nov 11 at 23:07










  • I did not know that MATLAB does zero-pads automatically, but it makes sense.
    – user4061624
    Nov 12 at 1:34








1




1




Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
– user10516309
Nov 11 at 18:37




Thank you. Something like xframed = [xframed; zeros(windowLength,1)]; And I apply the window to xframed and then the FFT, right? I'm using MATLAB.
– user10516309
Nov 11 at 18:37












Could you explain a little bit better the part of making the window length the power of 2? Why?
– user10516309
Nov 11 at 18:50






Could you explain a little bit better the part of making the window length the power of 2? Why?
– user10516309
Nov 11 at 18:50






1




1




FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
– Nikolay Shmyrev
Nov 11 at 21:09




FFT algorithm requires input to be power of 2, you can check dsp.stackexchange.com/questions/8792/… for example. Matlab zero-pads automatically, you do not need to pad yourself. See "If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n" in ch.mathworks.com/help/matlab/ref/fft.html
– Nikolay Shmyrev
Nov 11 at 21:09




1




1




That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
– user10516309
Nov 11 at 23:07




That makes sense, with or without the zero-padding (done by me), that is no different in the spectrogram. Thanks.
– user10516309
Nov 11 at 23:07












I did not know that MATLAB does zero-pads automatically, but it makes sense.
– user4061624
Nov 12 at 1:34




I did not know that MATLAB does zero-pads automatically, but it makes sense.
– user4061624
Nov 12 at 1:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250745%2fzero-padding-before-or-after-the-windowing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation