├── freqz_m.m ├── freqz_m_2pi.m ├── ideal_lp.m ├── matlab_tutorial ├── centeredFFT.m ├── fft_ex.m └── positiveFFT.m └── windowing ├── README.md ├── black_rp.bmp ├── hamm_rp.bmp ├── hann_rp.bmp ├── kaiser_rp.bmp ├── rectwin_rp.bmp ├── triang_rp.bmp └── windowing.m /freqz_m.m: -------------------------------------------------------------------------------- 1 | function [db,mag,pha,grd,w] = freqz_m(b,a) 2 | % Modified version of freqz subroutine 3 | % ------------------------------------ 4 | % [db,mag,pha,grd,w] = freqz_m(b,a); 5 | % db = Relative magnitude in dB computed over 0 to pi radians 6 | % mag = Absolute magnitude computed over 0 to pi radians 7 | % pha = Phase response in radians over 0 to pi radians 8 | % grd = Group delay over 0 to pi radians 9 | % w = 501 frequencu samples between 0 to pi radians 10 | % b = numerator polynomial of H(z) (for FIR: b=h) 11 | % a = denominator polynomial of H(z) (for FIR: a=[1]) 12 | % 13 | [H,w] = freqz(b,a,1000,'whole'); 14 | H = (H(1:1:501))';w = (w(1:1:501))'; 15 | mag = abs(H); 16 | db = 20*log10((mag+eps)/max(mag)); 17 | pha = angle(H); 18 | grd = grpdelay(b,a,w); -------------------------------------------------------------------------------- /freqz_m_2pi.m: -------------------------------------------------------------------------------- 1 | function [db,mag,pha,grd,w] = freqz_m_2pi(b,a) 2 | % Modified version of freqz subroutine 3 | % ------------------------------------ 4 | % [db,mag,pha,grd,w] = freqz_m(b,a); 5 | % db = Relative magnitude in dB computed over 0 to pi radians 6 | % mag = Absolute magnitude computed over 0 to pi radians 7 | % pha = Phase response in radians over 0 to pi radians 8 | % grd = Group delay over 0 to pi radians 9 | % w = 501 frequencu samples between 0 to pi radians 10 | % b = numerator polynomial of H(z) (for FIR: b=h) 11 | % a = denominator polynomial of H(z) (for FIR: a=[1]) 12 | % 13 | [H,w] = freqz(b,a,1000,'whole'); 14 | H = (H(1:1:1000))';w = (w(1:1:1000))'; 15 | mag = abs(H); 16 | db = 20*log10((mag+eps)/max(mag)); 17 | pha = angle(H); 18 | grd = grpdelay(b,a,w); -------------------------------------------------------------------------------- /ideal_lp.m: -------------------------------------------------------------------------------- 1 | function hd = ideal_lp(wc,M) 2 | % Ideal Lowpass filter computation 3 | % -------------------------------- 4 | % [hd] = ideal_lp(wc,M) 5 | % hd = ideal impulse response between 0 to M-1 6 | % wc = cutoff frequency in radians 7 | % M = length of the ideal filter 8 | % 9 | alpha = (M-1)/2; 10 | n = [0:1:(M-1)]; 11 | m = n - alpha + eps; % add smallest number to avoid divide by zero 12 | hd = sin(wc*m) ./ (pi*m); -------------------------------------------------------------------------------- /matlab_tutorial/centeredFFT.m: -------------------------------------------------------------------------------- 1 | function [X,freq]=centeredFFT(x,Fs) 2 | %this is a custom function that helps in plotting the two-sided spectrum 3 | %x is the signal that is to be transformed 4 | %Fs is the sampling rate 5 | 6 | N=length(x); 7 | 8 | %this part of the code generates that frequency axis 9 | if mod(N,2)==0 10 | k=-N/2:N/2-1; % N even 11 | else 12 | k=-(N-1)/2:(N-1)/2; % N odd 13 | end 14 | T=N/Fs; 15 | freq=k/T; %the frequency axis 16 | 17 | %takes the fft of the signal, and adjusts the amplitude accordingly 18 | X=fft(x)/N; % normalize the data 19 | X=fftshift(X); %shifts the fft data so that it is centered 20 | -------------------------------------------------------------------------------- /matlab_tutorial/fft_ex.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear all; 3 | close all; 4 | 5 | fo = 4; %frequency of the sine wave 6 | Fs = 100; %sampling rate 7 | Ts = 1/Fs; %sampling time interval 8 | t = 0:Ts:1-Ts; %sampling period 9 | n = length(t); %number of samples 10 | y = 2*sin(2*pi*fo*t); %the sine curve 11 | 12 | %plot the cosine curve in the time domain 13 | sinePlot = figure; 14 | plot(t,y) 15 | xlabel('time (seconds)') 16 | ylabel('y(t)') 17 | title('Sample Sine Wave') 18 | grid 19 | 20 | % %plot the frequency spectrum using the MATLAB fft command 21 | % matlabFFT = figure; %create a new figure 22 | % YfreqDomain = fft(y); %take the fft of our sin wave, y(t) 23 | % 24 | % stem(abs(YfreqDomain)); %use abs command to get the magnitude 25 | % %similary, we would use angle command to get the phase plot! 26 | % %we'll discuss phase in another post though! 27 | % 28 | % xlabel('Sample Number') 29 | % ylabel('Amplitude') 30 | % title('Using the Matlab fft command') 31 | % grid 32 | % axis([0,100,0,120]) 33 | % 34 | % [YfreqDomain,frequencyRange] = centeredFFT(y,Fs); 35 | % centeredFFT = figure; 36 | % 37 | % %remember to take the abs of YfreqDomain to get the magnitude! 38 | % stem(frequencyRange,abs(YfreqDomain)); 39 | % xlabel('Freq (Hz)') 40 | % ylabel('Amplitude') 41 | % title('Using the centeredFFT function') 42 | % grid 43 | % axis([-6,6,0,1.5]) 44 | 45 | [YfreqDomain,frequencyRange] = positiveFFT(y,Fs); 46 | positiveFFT = figure; 47 | stem(frequencyRange,abs(YfreqDomain)); 48 | set(positiveFFT,'Position',[500,500,500,300]) 49 | xlabel('Freq (Hz)') 50 | ylabel('Amplitude') 51 | title('Using the positiveFFT function') 52 | grid 53 | axis([0,20,0,1.5]) 54 | -------------------------------------------------------------------------------- /matlab_tutorial/positiveFFT.m: -------------------------------------------------------------------------------- 1 | function [X,freq]=positiveFFT(x,Fs) 2 | N=length(x); %get the number of points 3 | k=0:N-1; %create a vector from 0 to N-1 4 | T=N/Fs; %get the frequency interval 5 | freq=k/T; %create the frequency range 6 | X=fft(x)/N; % normalize the data 7 | 8 | %only want the first half of the FFT, since it is redundant 9 | cutOff = ceil(N/2); 10 | 11 | %take only the first half of the spectrum 12 | X = X(1:cutOff); 13 | freq = freq(1:cutOff); -------------------------------------------------------------------------------- /windowing/README.md: -------------------------------------------------------------------------------- 1 | # 窗函数实验 2 | > 本代码所使用到的子程序在上一级目录中 3 | 4 | 本代码生成常用的窗函数和它的频响波形 5 | -------------------------------------------------------------------------------- /windowing/black_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/black_rp.bmp -------------------------------------------------------------------------------- /windowing/hamm_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/hamm_rp.bmp -------------------------------------------------------------------------------- /windowing/hann_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/hann_rp.bmp -------------------------------------------------------------------------------- /windowing/kaiser_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/kaiser_rp.bmp -------------------------------------------------------------------------------- /windowing/rectwin_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/rectwin_rp.bmp -------------------------------------------------------------------------------- /windowing/triang_rp.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greedyhao/DSP/872befb98752d755a87a2c568e6cbd81d39ef2ec/windowing/triang_rp.bmp -------------------------------------------------------------------------------- /windowing/windowing.m: -------------------------------------------------------------------------------- 1 | close all; 2 | M = 50; 3 | n = [0:1:M-1]; 4 | hd = ideal_lp(0.2*pi,M); 5 | 6 | % rectangular 7 | w_rect = (rectwin(M))'; 8 | [db_rec,mag_rec,pha_rec,grd_rec,w_rec] = freqz_m_2pi(w_rect,[1]); 9 | h = hd .* w_rect; 10 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 11 | 12 | subplot(2,2,1); 13 | stem(n,w_rect);axis([-5 M+4 0 1.1]); 14 | xlabel('n');ylabel('w(n)'); 15 | title('Rectangular Window'); 16 | 17 | subplot(2,2,2); 18 | plot(w_rec/pi,fftshift(db_rec)); 19 | axis([0.5 1.5 -40 1]); 20 | xlabel('frequency in pi units');ylabel('decibels'); 21 | title('Amplitude Response in dB'); 22 | 23 | subplot(2,2,3); 24 | plot(w_rec/pi,fftshift(mag_rec)); 25 | axis([0.5 1.5 0 55]); 26 | xlabel('frequency in pi units');ylabel('Wr'); 27 | title('Amplitude Response'); 28 | 29 | subplot(2,2,4); 30 | plot(w_h/pi,fftshift(db_h)); 31 | axis([0 2 -50 10]); 32 | xlabel('frequency in pi units');ylabel('decibels'); 33 | title('Integrated Amplitude Response'); 34 | 35 | % triangular 36 | figure; 37 | w_triang = (triang(M))'; 38 | [db_tri,mag_tri,pha_tri,grd_tri,w_tri] = freqz_m_2pi(w_triang,[1]); 39 | h = hd .* w_triang; 40 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 41 | 42 | subplot(2,2,1); 43 | stem(n,w_triang);axis([-5 M+4 0 1.1]); 44 | xlabel('n');ylabel('w(n)'); 45 | title('Triangle Window'); 46 | 47 | subplot(2,2,2); 48 | plot(w_tri/pi,fftshift(db_tri)); 49 | axis([0.5 1.5 -200 1]); 50 | xlabel('frequency in pi units');ylabel('decibels'); 51 | title('Amplitude Response in dB'); 52 | 53 | subplot(2,2,3); 54 | plot(w_tri/pi,fftshift(mag_tri)); 55 | axis([0.5 1.5 0 30]); 56 | xlabel('frequency in pi units');ylabel('Wr'); 57 | title('Amplitude Response'); 58 | 59 | subplot(2,2,4); 60 | plot(w_h/pi,fftshift(db_h)); 61 | axis([0 2 -100 0]); 62 | xlabel('frequency in pi units');ylabel('decibels'); 63 | title('Integrated Amplitude Response'); 64 | 65 | % hanning 66 | figure; 67 | w_hann = (hann(M))'; 68 | [db_han,mag_han,pha_han,grd_han,w_han] = freqz_m_2pi(w_hann,[1]); 69 | h = hd .* w_hann; 70 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 71 | 72 | subplot(2,2,1); 73 | stem(n,w_hann);axis([-5 M+4 0 1.1]); 74 | xlabel('n');ylabel('w(n)'); 75 | title('Hanning Window'); 76 | 77 | subplot(2,2,2); 78 | plot(w_han/pi,fftshift(db_han)); 79 | axis([0.5 1.5 -150 0]); 80 | xlabel('frequency in pi units');ylabel('decibels'); 81 | title('Amplitude Response in dB'); 82 | 83 | subplot(2,2,3); 84 | plot(w_han/pi,fftshift(mag_han)); 85 | axis([0.5 1.5 0 30]); 86 | xlabel('frequency in pi units');ylabel('Wr'); 87 | title('Amplitude Response'); 88 | 89 | subplot(2,2,4); 90 | plot(w_h/pi,fftshift(db_h)); 91 | axis([0 2 -150 0]); 92 | xlabel('frequency in pi units');ylabel('decibels'); 93 | title('Integrated Amplitude Response'); 94 | 95 | % hamming 96 | figure; 97 | w_hamm = (hamming(M))'; 98 | [db_ham,mag_ham,pha_ham,grd_ham,w_ham] = freqz_m_2pi(w_hamm,[1]); 99 | h = hd .* w_hamm; 100 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 101 | 102 | subplot(2,2,1); 103 | stem(n,w_hamm);axis([-5 M+4 0 1.1]); 104 | xlabel('n');ylabel('w(n)'); 105 | title('Hamming Window'); 106 | 107 | subplot(2,2,2); 108 | plot(w_ham/pi,fftshift(db_ham)); 109 | axis([0.5 1.5 -150 0]); 110 | xlabel('frequency in pi units');ylabel('decibels'); 111 | title('Amplitude Response in dB'); 112 | 113 | subplot(2,2,3); 114 | plot(w_ham/pi,fftshift(mag_ham)); 115 | axis([0.5 1.5 0 30]); 116 | xlabel('frequency in pi units');ylabel('Wr'); 117 | title('Amplitude Response'); 118 | 119 | subplot(2,2,4); 120 | plot(w_h/pi,fftshift(db_h)); 121 | axis([0 2 -150 0]); 122 | xlabel('frequency in pi units');ylabel('decibels'); 123 | title('Integrated Amplitude Response'); 124 | 125 | % black 126 | figure; 127 | w_black = (blackman(M))'; 128 | [db_bla,mag_bla,pha_bla,grd_bla,w_bla] = freqz_m_2pi(w_black,[1]); 129 | h = hd .* w_black; 130 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 131 | 132 | subplot(2,2,1); 133 | stem(n,w_black);axis([-5 M+4 0 1.1]); 134 | xlabel('n');ylabel('w(n)'); 135 | title('Blackman Window'); 136 | 137 | subplot(2,2,2); 138 | plot(w_bla/pi,fftshift(db_bla)); 139 | axis([0.5 1.5 -200 0]); 140 | xlabel('frequency in pi units');ylabel('decibels'); 141 | title('Amplitude Response in dB'); 142 | 143 | subplot(2,2,3); 144 | plot(w_bla/pi,fftshift(mag_bla)); 145 | axis([0.5 1.5 0 25]); 146 | xlabel('frequency in pi units');ylabel('Wr'); 147 | title('Amplitude Response'); 148 | 149 | subplot(2,2,4); 150 | plot(w_h/pi,fftshift(db_h)); 151 | axis([0 2 -200 0]); 152 | xlabel('frequency in pi units');ylabel('decibels'); 153 | title('Integrated Amplitude Response'); 154 | 155 | % kaiser 156 | figure; 157 | w_kaiser = (kaiser(M,8.5))'; 158 | [db_kai,mag_kai,pha_kai,grd_kai,w_kai] = freqz_m_2pi(w_kaiser,[1]); 159 | h = hd .* w_kaiser; 160 | [db_h,mag_h,pha_h,grd_h,w_h] = freqz_m_2pi(h,[1]); 161 | 162 | subplot(2,2,1); 163 | stem(n,w_kaiser);axis([-5 M+4 0 1.1]); 164 | xlabel('n');ylabel('w(n)'); 165 | title('Kaiser Window \beta = 8.5'); 166 | 167 | subplot(2,2,2); 168 | plot(w_kai/pi,fftshift(db_bla)); 169 | axis([0.5 1.5 -200 0]); 170 | xlabel('frequency in pi units');ylabel('decibels'); 171 | title('Amplitude Response in dB'); 172 | 173 | subplot(2,2,3); 174 | plot(w_kai/pi,fftshift(mag_bla)); 175 | axis([0.5 1.5 0 25]); 176 | xlabel('frequency in pi units');ylabel('Wr'); 177 | title('Amplitude Response'); 178 | 179 | subplot(2,2,4); 180 | plot(w_h/pi,fftshift(db_h)); 181 | axis([0 2 -200 0]); 182 | xlabel('frequency in pi units');ylabel('decibels'); 183 | title('Integrated Amplitude Response'); 184 | --------------------------------------------------------------------------------