17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This is done by generating a lowpass filter and then spectrally inverting it<br />

procedure wsfirHP(var H : TDoubleArray; const WindowType : TWindowType; const CutOff : Double);<br />

var i : Integer;<br />

begin<br />

wsfirLP(H, WindowType, CutOff); // 1. Generate lowpass filter<br />

// 2. Spectrally invert (negate all samples and add 1 to center sample) lowpass filter<br />

// = delta[n-((N-1)/2)] - h[n], in the time domain<br />

for i:=0 to Length(H)-1<br />

do H[i]:=H[i]*-1.0;<br />

H[(Length(H)-1) div 2]:=H[(Length(H)-1) div 2]+1.0;<br />

end;<br />

// Generate bandstop filter<br />

// This is done by generating a lowpass and highpass filter and adding them<br />

procedure wsfirBS(var H : TDoubleArray; const WindowType : TWindowType; const LowCutOff, HighCutOff : Double);<br />

var i : Integer;<br />

H2 : TDoubleArray;<br />

begin<br />

SetLength(H2,Length(H));<br />

// 1. Generate lowpass filter at first (low) cutoff frequency<br />

wsfirLP(H, WindowType, LowCutOff);<br />

// 2. Generate highpass filter at second (high) cutoff frequency<br />

wsfirHP(H2, WindowType, HighCutOff);<br />

// 3. Add the 2 filters together<br />

for i:=0 to Length(H)-1<br />

do H[i]:=H[i]+H2[i];<br />

SetLength(H2,0);<br />

end;<br />

// Generate bandpass filter<br />

// This is done by generating a bandstop filter and spectrally inverting it<br />

procedure wsfirBP(var H : TDoubleArray; const WindowType : TWindowType; const LowCutOff, HighCutOff : Double);<br />

var i : Integer;<br />

begin<br />

wsfirBS(H, WindowType, LowCutOff, HighCutOff); // 1. Generate bandstop filter<br />

// 2. Spectrally invert (negate all samples and add 1 to center sample) lowpass filter<br />

// = delta[n-((N-1)/2)] - h[n], in the time domain<br />

for i:=0 to Length(H)-1<br />

do H[i]:=H[i]*-1.0;<br />

H[(Length(H)-1) div 2]:=H[(Length(H)-1) div 2]+1.0;<br />

end;<br />

// Generate sinc function - used for making lowpass filter<br />

procedure genSinc(var Sinc : TDoubleArray; const Cutoff : Double);<br />

var i,j : Integer;<br />

n,k : Double;<br />

begin<br />

j:=Length(Sinc)-1;<br />

k:=1/j;<br />

// Generate sinc delayed by (N-1)/2<br />

for i:=0 to j do<br />

if (i=j div 2)<br />

then Sinc[i]:=2.0*Cutoff<br />

else<br />

begin<br />

n:=i-j/2.0;<br />

Sinc[i]:=sin(2.0*PI*Cutoff*n)/(PI*n);<br />

end;<br />

end;<br />

// Generate window function (Gaussian)<br />

procedure wGaussian(var W : TDoubleArray);<br />

var i,j : Integer;<br />

k : Double;<br />

begin<br />

j:=Length(W)-1;<br />

k:=1/j;<br />

for i:=0 to j<br />

do W[i]:=W[i]*(exp(-5.0/(sqr(j))*(2*i-j)*(2*i-j)));<br />

end;<br />

// Generate window function (Blackman-Harris)<br />

procedure wBlackmanHarris(var W : TDoubleArray);<br />

var i,j : Integer;<br />

k : Double;<br />

begin<br />

j:=Length(W)-1;<br />

k:=1/j;<br />

for i:=0 to j

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!