Friday 6 May 2016

filters - 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;

Answer




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);

[inBuffer, Fs] = wavread(inFile);

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);

No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...