# how to apply Short-time fourier transform to code in matlab

Please Help, I have a white noise signal that I created and need help to apply the Short-time fourier transform to my code so that it can do the bandpass filtering. I am trying to put it in the code so that I don't have to use the FDA tool. Also be able to plot the graphs from the output of the STFT being applied.

L = 5000; %Sample length for the random signal
Pause = 10000; %Sample Pause Gap
mu = 0;
sigma = 2;

%Need to see left signal is not displaying
Left_signal = sigma*randn(L,1) + mu;
Right_signal = sigma*randn(L,1) + mu;

Long_signal = [Left_signal zeros(L,1); zeros(Pause,2); zeros(L,1) Right_signal];

%Plots subplots in graph
figure
subplot(211);
plot(Left_signal, 'b'); grid on;
subplot(212);
plot(Right_signal, 'r'); grid on;

• need to be able to learn how to window off "frames" of data using a complementary window (like the Hann). then you need to be able to, without (yet) using any FFT, recombine the frames to get your original signal back. once you can do that, then the STFT is simply the DFT (a.k.a. FFT) of each frame. Commented May 27, 2014 at 18:14
• how would I do that @robertbristow-johnson because I using the FDA tool for filtering at first then come to find out that was wrong for my class project and professor is killing me for it Commented May 27, 2014 at 18:16
• well, i don't know the FDA tool. i'll post a very skeletal piece of MATLAB. it might help. Commented May 27, 2014 at 19:12
• Basically, STFT is Discrete Fourier Transform of the signal multiplied by the windowing function. Commented May 28, 2014 at 14:21
• I suggest you to also refer to this answer.
– jojeck
Commented May 30, 2014 at 7:58

this is very simplistic, but it

1. inputs a wav file and uses only one channel of it
2. breaks it up into windowed frames,
3. transforms it to the frequency domain,
4. does nothing to it in the frequency domain,
5. transforms back to the time domain,
6. overlap-adds that frame back into a time-domain array, and
7. eventually writes it back out to a different .wave file.

try it out. can't guarantee there ain't typos.


if ~exist('inFile')
inFile = 'input.wav';
end

if ~exist('outFile')
outFile = 'output.wav';
end

if ~exist('frameWidth')
frameWidth = 4096;          % size of FFT frame, better be a power of 2
end
frameHop = frameWidth/2;

analWindow = hanning(frameWidth);

x = [inBuffer(:,1); linspace(0, 0, frameWidth)'];                   % use left channel only, zeropad one frame at the end

clear inBuffer;

numSamples = length(x);

numFrames = floor((numSamples-frameWidth)/frameHop);

y = linspace(0, 0, numSamples)';

n = 0;                              % init sample pointer.  unlike MATLAB, i like counting from 0

for frameIndex = 1:numFrames

xWindowed = x(n+1:n+frameWidth) .* analWindow;     % get and window the input audio frame

X = fft(fftshift(xWindowed));              % do the FFT

Y = X;                         % copy the input spectrum to output

% do whatever processing to Y that you like

yWindowed = fftshift(real(ifft(Y)));           % convert back to time domain, toss the imaginary part

y(n+1:n+frameWidth) = y(n+1:n+frameWidth) + yWindowed;

n = n + frameHop;
end

wavwrite(y, Fs, outFile);

• what size is "size" referring from numsamples = wavread(inFile, 'size'); Commented Jun 2, 2014 at 19:34
• i think it's a text literal argument that tells wavread to return the number of samples of the file with file name in inFile. with that argument, it doesn't actually read in the file. not yet. Commented Jun 3, 2014 at 6:34
• with the new edit in the code I keep getting this error "Index exceeds matrix dimensions. Error in soundRecord (line 43) xWindowed = x(n+1:n+frameWidth) .* analWindow; % get and window the input audio frame" and i tried to look it up because I wanted to plot the outcome as well just didn't get that far since kept getting an error @robert bristow-johnson Commented Jun 3, 2014 at 18:12
• i guess we should change it back to numFrames = floor(numSamples/frameHop) - 1; Commented Jun 4, 2014 at 3:11