├── .gitignore ├── DFT_sequence.png ├── README.md ├── _config.yml ├── basic_matlab ├── Vector.png ├── basics.m ├── matrix.m ├── matrix.png ├── polynomial.m ├── polynomial.png └── stem.png ├── basic_signals ├── Dirac Delta.png ├── Random Noise.png ├── Sine Wave.png ├── Unit Ramp.png ├── Unit Step.png ├── dirac_mine.m ├── epowerx.m ├── exponential.png ├── random_mine.m ├── sine.m ├── unit_ramp.m └── unit_step.m ├── convolution.m ├── convolutions.png ├── dft8.m ├── dft8loop.png ├── fft8pt.m ├── filters ├── Spec-anaysis-for-fir.png ├── butter8pass.png ├── butter_8_zplot.png ├── butter_bandpass_2order.m ├── butter_pass_8.m ├── butterphase8pass.png ├── butterworthpass.png ├── butterworthphase.png ├── cheb2_high.png ├── cheb2_high_dB.png ├── cheby1.png ├── chebyshev1.m ├── chebyshev2.m ├── ellip_low.png ├── ellip_low_dB.png ├── elliptic.m ├── filter_type.m ├── filtertypes.png ├── fir-filtering.png ├── fir-hamming-phase.png ├── fir-hamming.png ├── forderdemonet.png ├── ideal_filter.m ├── idealfilter.png ├── increasing_order.m ├── low_elliptic.m ├── oderdemo.png ├── signal filtering.png └── sine_filter.m ├── fourier_demo.m ├── hamming_low_fir.m ├── low-FIR-hamming.m ├── sampling.png ├── sampling_sine.m ├── sinefft.png ├── sinefourier.m ├── sources.txt └── zdemo.png /.gitignore: -------------------------------------------------------------------------------- 1 | convolution.asv 2 | *.asv 3 | *.docx 4 | *.pdf -------------------------------------------------------------------------------- /DFT_sequence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/DFT_sequence.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Digital Signal Processing Using MATLAB 2 | --- 3 | ## This repo consists programs made on MATLAB for digital signal processing (DSP) undergraduate course. 4 | 5 | ## The Book based on the code along with all the explanation can be found on [DSP gitbook](https://www.gitbook.com/book/kshitijpurwar/dsp-lab-file/details) 6 | 7 | #### Basic Signals folder has 6 basic signals along with their graphs in both continuous and discrete form. 8 | 1. Unit step (**heaviside**) 9 | 2. Unit ramp 10 | 3. Sine wave 11 | 4. Unit Impulse 12 | 5. Decaying Exponential 13 | 6. Random Noise 14 | 15 | #### Given below are examples of the graphs in this repo. 16 | ![Decaying Exponential](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/basic_signals/exponential.png) 17 | 18 | ![Sine Wave](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/basic_signals/Sine%20Wave.png) 19 | 20 | #### Other programs included in this repo are : 21 | 1. Spectral Analysis of a sinusoidal function 22 | ![Analysis](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/sinefft.png) 23 | 24 | 2. Sampling theorem demonstartion [Nyquist-Shannon](https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem) . 25 | ![Sampling](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/sampling.png) 26 | 3. 8 point DFT of a sequence. 27 | ![8 point DFT](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/DFT_sequence.png) 28 | 4. Convolution Demo (circular and linear). 29 | ![Convolution](https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/master/convolutions.png) 30 | **Many More programs to come in future**. -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-tactile -------------------------------------------------------------------------------- /basic_matlab/Vector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_matlab/Vector.png -------------------------------------------------------------------------------- /basic_matlab/basics.m: -------------------------------------------------------------------------------- 1 | %Some Basic MATLAB commands 2 | clc ; 3 | clear; 4 | V = [1 2 4 6 2 4]; 5 | A=4+3*i; 6 | display(['The Magnitude of A ',num2str(A), ' is ',num2str(abs(A)),' and the phase is ',num2str(angle(A)*180/pi)]) 7 | display(['The vector V is ',num2str(V)]); 8 | stem(V,'black','filled'); 9 | xlabel('Index ( N ) ') 10 | title({'The plot of vector V ',num2str(V)}) 11 | ylabel('Magnitude') 12 | 13 | -------------------------------------------------------------------------------- /basic_matlab/matrix.m: -------------------------------------------------------------------------------- 1 | %Basic matrix operations in Matlab 2 | clc; 3 | A=[1 2 3 ; 5 3 2 ; 6 9 0]; 4 | At=A'; 5 | I=eye(3); 6 | Z=zeros(3); 7 | display('The Matrix A is :') 8 | display(A) 9 | display(['Size of matrix A is ',num2str(size(A)),' (Rows Columns)']) 10 | display('The 3 X 3 Identity Matrix I is :') 11 | display(I); 12 | display('The Transpose of Matrix A is :') 13 | display(At); 14 | -------------------------------------------------------------------------------- /basic_matlab/matrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_matlab/matrix.png -------------------------------------------------------------------------------- /basic_matlab/polynomial.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | x=-1:0.01:5; 3 | y=4*x.^4-26*x.^3+30*x.^2+20*x; 4 | plot(x,y,'black','linesmoothing','on'); 5 | grid on; 6 | title('Plot of 4x^{4} - 26x^{3} + 30x^{2} + 20x'); 7 | xlabel('X --------------------------->'); 8 | ylabel('Y(x) ------------------------>'); 9 | 10 | print('polynomial','-dpng') 11 | 12 | -------------------------------------------------------------------------------- /basic_matlab/polynomial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_matlab/polynomial.png -------------------------------------------------------------------------------- /basic_matlab/stem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_matlab/stem.png -------------------------------------------------------------------------------- /basic_signals/Dirac Delta.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/Dirac Delta.png -------------------------------------------------------------------------------- /basic_signals/Random Noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/Random Noise.png -------------------------------------------------------------------------------- /basic_signals/Sine Wave.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/Sine Wave.png -------------------------------------------------------------------------------- /basic_signals/Unit Ramp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/Unit Ramp.png -------------------------------------------------------------------------------- /basic_signals/Unit Step.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/Unit Step.png -------------------------------------------------------------------------------- /basic_signals/dirac_mine.m: -------------------------------------------------------------------------------- 1 | %The Code is for dirac delta function 2 | t=-1:0.01:1; 3 | y=double(t==0); 4 | subplot(2,1,1); 5 | plot(t,y,'b'); 6 | xlabel('time in seconds'); 7 | ylabel('y(t)'); 8 | title('Plot of continuous dirac delta function from -1 to 1'); 9 | 10 | %Discrete dirac delta ramp 11 | n = -20:1:20; 12 | y_n= double(n==0); 13 | subplot(2,1,2); 14 | stem(n,y_n,'b','filled'); 15 | xlabel('n i.e. Sample number'); 16 | ylabel('y(n)'); 17 | title('Plot of discrete dirac delta function from -1 to 1 with 41 samples'); 18 | 19 | print('Dirac Delta','-dpng') -------------------------------------------------------------------------------- /basic_signals/epowerx.m: -------------------------------------------------------------------------------- 1 | t=0:0.01:1; 2 | y=exp(-5*t); 3 | subplot(2,1,1); 4 | plot(t,y,'g'); 5 | title('Decaying Exponential function Y(t)=e^{-6x}'); 6 | xlabel('Time t (sec)'); 7 | ylabel('Y(t)') 8 | 9 | 10 | n=0:1:50; 11 | e=exp(-5*n/50); 12 | subplot(2,1,2); 13 | stem(n,e,'go','filled'); 14 | title('Discrete Decaying Exponential function Y(n)=e^{-6n}'); 15 | xlabel('Sample Number n '); 16 | ylabel('Y(n)') 17 | 18 | print('exponential','-dpng') 19 | -------------------------------------------------------------------------------- /basic_signals/exponential.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/basic_signals/exponential.png -------------------------------------------------------------------------------- /basic_signals/random_mine.m: -------------------------------------------------------------------------------- 1 | %The Code is for Random wave 2 | t=-10:0.1:10; 3 | y=rand(size(t)); 4 | subplot(2,1,1); 5 | plot(t,y,'b'); 6 | xlabel('time in seconds'); 7 | ylabel('y(t)'); 8 | title('Plot of continuous Random Noise '); 9 | 10 | %Discrete dirac delta ramp 11 | n = -20:1:20; 12 | y_n= rand(size(n)); 13 | subplot(2,1,2); 14 | stem(n,y_n,'b','filled'); 15 | xlabel('n i.e. Sample number'); 16 | ylabel('y(n)'); 17 | title('Plot of discrete Random noise with 41 samples'); 18 | 19 | print('Random Noise','-dpng') -------------------------------------------------------------------------------- /basic_signals/sine.m: -------------------------------------------------------------------------------- 1 | %The Code is for continuous sine wave 2 | t=0:0.01:2; 3 | y=sin(pi*2*t); 4 | subplot(2,1,1); 5 | plot(t,y,'b'); 6 | xlabel('time in seconds'); 7 | ylabel('Sine wave amplitude'); 8 | title('Plot of sin(2\pit) continuous wave from 0 to 4\pi'); 9 | 10 | %Discrete Sine wave 11 | n = 0:1:40; 12 | y= sin(.1*pi*n); 13 | subplot(2,1,2); 14 | stem(n,y,'b','filled'); 15 | xlabel('n i.e. Sample number'); 16 | ylabel('Sine wave amplitude'); 17 | title('Plot of sin(2\pin) discrete wave from 0 to 4\pi with 41 samples'); 18 | 19 | print('Sine Wave','-dpng') -------------------------------------------------------------------------------- /basic_signals/unit_ramp.m: -------------------------------------------------------------------------------- 1 | %The Code is for continuous unit ramp 2 | t=-1:0.01:1; 3 | y=heaviside(t).*t; 4 | subplot(2,1,1); 5 | plot(t,y,'b'); 6 | xlabel('time in seconds'); 7 | ylabel('y(t)'); 8 | title('Plot of continuous unit ramp function from -1 to 1'); 9 | 10 | %Discrete unit ramp 11 | n = -20:1:20; 12 | y_n= heaviside(.05*n).*n; 13 | subplot(2,1,2); 14 | stem(n,y_n,'b','filled'); 15 | xlabel('n i.e. Sample number'); 16 | ylabel('y(n)'); 17 | title('Plot of discrete unit ramp function from -1 to 1 with 41 samples'); 18 | 19 | print('Unit Ramp','-dpng') -------------------------------------------------------------------------------- /basic_signals/unit_step.m: -------------------------------------------------------------------------------- 1 | %The Code is for continuous unit step 2 | t=-1:0.01:1; 3 | y=heaviside(t); 4 | subplot(2,1,1); 5 | plot(t,y,'b'); 6 | xlabel('time in seconds'); 7 | ylabel('y(t)'); 8 | title('Plot of continuous heaviside unit step function from -1 to 1'); 9 | 10 | %Discrete unit step 11 | n = -20:1:20; 12 | y= heaviside(.05*n); 13 | subplot(2,1,2); 14 | stem(n,y,'b','filled'); 15 | xlabel('n i.e. Sample number'); 16 | ylabel('y(n)'); 17 | title('Plot of discrete heaviside unit step function from -1 to 1'); 18 | 19 | print('Unit Step','-dpng') -------------------------------------------------------------------------------- /convolution.m: -------------------------------------------------------------------------------- 1 | %Linear Convolution 2 | x=[2,1,3,5]; 3 | y=[2,4,1]; 4 | lc=conv(x,y); 5 | 6 | 7 | subplot(3,2,1); 8 | stem(x,'filled'); 9 | title('Sequence x : [2,1,3,5]'); 10 | xlabel('n i.e. sample number'); 11 | ylabel('X(n)'); 12 | 13 | 14 | subplot(3,2,3); 15 | stem(y,'filled'); 16 | title('Sequence y : [2,4,1]'); 17 | xlabel('n i.e. sample number'); 18 | ylabel('Y(n)'); 19 | 20 | subplot(3,2,5); 21 | stem(lc,'filled'); 22 | xlabel('n i.e. sample number'); 23 | title({'Linear Convolution of Sequence x and y',num2str(lc)}); 24 | ylabel('Convoluted Signal'); 25 | 26 | %Circular Convolution 27 | x1=[1,4,6,7]; 28 | y1=[2,9,0,4]; 29 | cc=cconv(x,y,4); 30 | 31 | subplot(3,2,2); 32 | stem(x1,'filled'); 33 | title('Sequence x1 : [1,4,6,7]'); 34 | xlabel('n i.e. sample number'); 35 | ylabel('X1(n)'); 36 | 37 | subplot(3,2,4); 38 | stem(y1,'filled'); 39 | title('Sequence y1 : [2,9,0,4]'); 40 | xlabel('n i.e. sample number'); 41 | ylabel('Y1(n)'); 42 | 43 | subplot(3,2,6); 44 | stem(cc,'filled'); 45 | xlabel('n i.e. sample number'); 46 | title({'Circular Convolution of Sequence x1 and y1';num2str(cc)}); 47 | ylabel('Convoluted Signal'); 48 | 49 | print('convolutions','-dpng'); 50 | 51 | -------------------------------------------------------------------------------- /convolutions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/convolutions.png -------------------------------------------------------------------------------- /dft8.m: -------------------------------------------------------------------------------- 1 | % Finding * point DFT using a loop 2 | N=8; 3 | x=[ 2 -1 1 3 -2 1 2 0]; 4 | n=-0:N-1; 5 | for k=0:N-1 6 | W=exp(-j*2*pi*k*n/N); % Twiddle factor 7 | prod=x.*W; 8 | X(k+1)=sum(prod); 9 | end 10 | 11 | subplot(2,1,1) 12 | stem(n,abs(X),'black','filled'); 13 | title('Magnitude spectrum of 8 point DFT'); 14 | 15 | subplot(2,1,2) 16 | stem(n,abs(fft(x,8)),'black','filled'); 17 | title('Magnitude spectrum of 8 point DFT using fft command.'); -------------------------------------------------------------------------------- /dft8loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/dft8loop.png -------------------------------------------------------------------------------- /fft8pt.m: -------------------------------------------------------------------------------- 1 | clear; 2 | x=[ 2 -1 1 3 -2 1 2 0]; 3 | subplot(2,2,1) 4 | stem(x,'b','filled'); 5 | grid on; 6 | axis([0 9 -3 4 ]); 7 | title({'Sequence X(n): ';num2str(x)}); 8 | xlabel('Sample Number (n)') 9 | ylabel('Value'); 10 | 11 | y=fft(x,8); 12 | real_y=real(y); 13 | imag_y=imag(y); 14 | 15 | subplot(2,2,2) 16 | stem(real_y,'o','filled'); 17 | grid on; 18 | axis([0 9 min(real_y)-1 max(real_y)+1]); 19 | title({'Real part of 8-point DFT: ';num2str(round(real_y*10)/10)}); 20 | xlabel('Sample Number (n)') 21 | ylabel('Value of Real part'); 22 | 23 | plot3=subplot(2,2,3) 24 | stem(imag_y,'o','filled'); 25 | plot3.XTickMode='auto'; 26 | grid on; 27 | axis([0 9 min(imag_y)-1 max(imag_y)+1]); 28 | title('hooola') 29 | title({'Imaginary part of 8-point DFT of Sequence X(n) ';num2str(round(imag_y*10)/10)}); 30 | xlabel('Sample Number (n)') 31 | ylabel('Value of Imaginary part '); 32 | 33 | inverse_y=ifft(y,8); 34 | plot4=subplot(2,2,4) 35 | stem(inverse_y,'o','filled'); 36 | grid on; 37 | axis([0 9 min(inverse_y)-1 max(inverse_y)+1]); 38 | title({'IInvesre of 8-point DFT: ';num2str(round(inverse_y*10)/10)}); 39 | xlabel('Sample Number (n)') 40 | ylabel('Value '); 41 | 42 | print('DFT_sequence','-dpng'); -------------------------------------------------------------------------------- /filters/Spec-anaysis-for-fir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/Spec-anaysis-for-fir.png -------------------------------------------------------------------------------- /filters/butter8pass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/butter8pass.png -------------------------------------------------------------------------------- /filters/butter_8_zplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/butter_8_zplot.png -------------------------------------------------------------------------------- /filters/butter_bandpass_2order.m: -------------------------------------------------------------------------------- 1 | [b a]=butter(2,[0.4 0.7],'bandpass'); 2 | [H,w]=freqz(b,a); 3 | figure(1); 4 | plot(w/pi,abs(H),'r','linewidth',2,'linesmoothing','on'); 5 | axis([0 1 0 1.1]); 6 | grid on; 7 | title(['Amplitude Response of a butterworth bandpass filter with band edges at ',num2str(.4),' and ',num2str(.7)]); 8 | xlabel('Frequency (rad/sample)'); 9 | ylabel('Gain') 10 | 11 | figure(2); 12 | 13 | plot(w,angle(H),'black','linewidth',2,'linesmoothing','on'); 14 | title(['Amplitude Response of a butterworth bandpass filter with band edges at ',num2str(.4),' and ',num2str(.7)]); 15 | xlabel('Frequency (rad/sample)'); 16 | ylabel('Phase (rad)'); 17 | grid on; -------------------------------------------------------------------------------- /filters/butter_pass_8.m: -------------------------------------------------------------------------------- 1 | [b a]=butter(8,[0.4 0.7],'bandpass'); 2 | [H,w]=freqz(b,a); 3 | figure(1); 4 | plot(w/pi,abs(H),'black','linewidth',2,'linesmoothing','on'); 5 | axis([0 1 0 1.1]); 6 | grid on; 7 | title(['Amplitude Response of a 8th order butterworth bandpass filter with band edges at ',num2str(.4),' and ',num2str(.7)]); 8 | xlabel('Frequency (rad/sample)'); 9 | ylabel('Gain'); 10 | display(['Denominator coefficients are :',num2str(round(b*100)/100)]) 11 | 12 | display(['Numerator coefficients are :',num2str(round(a*100)/100)]) 13 | 14 | figure(2); 15 | 16 | plot(w,angle(H),'black','linewidth',2,'linesmoothing','on'); 17 | title(['Amplitude Response of a 8th order butterworth bandpass filter with band edges at ',num2str(.4),' and ',num2str(.7)]); 18 | xlabel('Frequency (rad/sample)'); 19 | ylabel('Phase (rad)'); 20 | grid on; 21 | figure(3); 22 | zplane(b,a,'black') 23 | title('8th-Order Butterworth band pass Digital Filter'); 24 | legend('zeros','poles') -------------------------------------------------------------------------------- /filters/butterphase8pass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/butterphase8pass.png -------------------------------------------------------------------------------- /filters/butterworthpass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/butterworthpass.png -------------------------------------------------------------------------------- /filters/butterworthphase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/butterworthphase.png -------------------------------------------------------------------------------- /filters/cheb2_high.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/cheb2_high.png -------------------------------------------------------------------------------- /filters/cheb2_high_dB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/cheb2_high_dB.png -------------------------------------------------------------------------------- /filters/cheby1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/cheby1.png -------------------------------------------------------------------------------- /filters/chebyshev1.m: -------------------------------------------------------------------------------- 1 | %Chebyshev filter type 2 | clear all; 3 | %Low pass Filter 4 | [b_low a_low]=cheby1(3,1,.3,'low'); 5 | [H_low,w_low]=freqz(b_low,a_low); 6 | subplot(2,2,1); 7 | plot(w_low/pi,abs(H_low),'black','linewidth',1,'linesmoothing','on'); 8 | title({'Magnitude response of a 3rd order Low pass Chebyshev-1 filter with Wc at .3','with ripple of 1 Db in passband'}); 9 | xlabel('Normalized frequency'); 10 | ylabel('Gain'); 11 | %axis([0 1 0 1.1]); 12 | 13 | %high pass Filter 14 | 15 | [b_high a_high]=cheby1(3,1.5,.7,'high'); 16 | [H_high,w_high]=freqz(b_high,a_high); 17 | subplot(2,2,2); 18 | plot(w_high/pi,abs(H_high),'black','linewidth',1,'linesmoothing','on'); 19 | title({'Magnitude response of a 3rd order High pass Chebyshev-1 filter with Wc at 0.7',' with ripple of 1.5 Db in passband'}); 20 | xlabel('Normalized frequency'); 21 | ylabel('Gain'); 22 | axis([0 1 0 1.1]); 23 | 24 | 25 | % %band pass Filter 26 | [b_pass a_pass]=cheby1(3,.3,[.3 .7],'bandpass'); 27 | [H_pass,w_pass]=freqz(b_pass,a_pass); 28 | subplot(2,2,3); 29 | plot(w_pass/pi,abs(H_pass),'black','linewidth',1,'linesmoothing','on'); 30 | title({'Magnitude response of a 3rd order Band pass Chebyshev-1 filter with edges at 0.3 and 0.7 ','with ripple of .3 Db in passband'}); 31 | xlabel('Normalized frequency'); 32 | ylabel('Gain'); 33 | axis([0 1 0 1.1]); 34 | 35 | 36 | % %Band stop Filter 37 | [b_stop a_stop]=cheby1(3,.8,[.3 .8],'stop'); 38 | [H_stop,w_stop]=freqz(b_stop,a_stop); 39 | subplot(2,2,4); 40 | plot(w_stop/pi,abs(H_stop),'black','linewidth',1,'linesmoothing','on'); 41 | title({'Magnitude response of a 3rd order Band pass Chebyshev-1 filter with edges at 0.3 and 0.7 ','with ripple of 0.8 Db in passband'}); 42 | xlabel('Normalized frequency'); 43 | ylabel('Gain'); 44 | axis([0 1 0 1.1]); 45 | 46 | %print('filtertypes','-dpng'); -------------------------------------------------------------------------------- /filters/chebyshev2.m: -------------------------------------------------------------------------------- 1 | %Chebyshev 2 high pass 2 | clear all; 3 | [N,Wn]=cheb2ord(0.5,0.4,0.5,60); 4 | [b,a] = cheby2(N ,60,Wn,'high'); 5 | [h w]=freqz(b,a,256); 6 | H=abs(h); 7 | HdB=20*log10(H); 8 | figure (1); 9 | plot(w/pi,H,'black','linesmoothing','on'); 10 | grid on; 11 | title('Magnitude Repsonse of a Chebyshev I High pass filter with Wp=0.4 and Ws=0.5 and attenuation at 60dB in stopband'); 12 | xlabel('Normailized Frequency ( rad/sec )'); 13 | ylabel('Gain'); 14 | axis([0 1 0 1.1]); 15 | print('cheb2_high','-dpng'); 16 | 17 | 18 | figure (2); 19 | plot(w/pi,HdB,'black','linesmoothing','on'); 20 | grid on; 21 | title('Magnitude Repsonse of a Chebyshev I High pass filter with Wp=0.4 and Ws=0.5 and ripple at 60dB in stopband'); 22 | xlabel('Normailized Frequency ( rad/sec )'); 23 | ylabel('Gain in dBs'); 24 | axis([0 1 -120 2]); 25 | 26 | print('cheb2_high_dB','-dpng'); -------------------------------------------------------------------------------- /filters/ellip_low.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/ellip_low.png -------------------------------------------------------------------------------- /filters/ellip_low_dB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/ellip_low_dB.png -------------------------------------------------------------------------------- /filters/elliptic.m: -------------------------------------------------------------------------------- 1 | [b a]=ellip(2,1,20,.4); 2 | [h w]=freqz(b,a); 3 | figure(1); 4 | plot(w,abs(h)); 5 | figure(2); 6 | zplane(b,a); -------------------------------------------------------------------------------- /filters/filter_type.m: -------------------------------------------------------------------------------- 1 | %Butterworth filter type 2 | clear all; 3 | %Low pass Filter 4 | [b_low a_low]=butter(3,.3,'low'); 5 | [H_low,w_low]=freqz(b_low,a_low); 6 | subplot(2,2,1); 7 | plot(w_low/pi,abs(H_low),'black','linewidth',2,'linesmoothing','on'); 8 | title('Magnitude response of a 3rd order Low pass butterworth filter with Wc at .3'); 9 | xlabel('Normalized frequency'); 10 | ylabel('Gain'); 11 | axis([0 1 0 1.1]); 12 | 13 | 14 | %high pass Filter 15 | [b_high a_high]=butter(3,.6,'high'); 16 | [H_high,w_high]=freqz(b_high,a_high); 17 | subplot(2,2,2); 18 | plot(w_high/pi,abs(H_high),'black','linewidth',2,'linesmoothing','on'); 19 | title('Magnitude response of a 3rd order High pass butterworth filter with Wc at .6'); 20 | xlabel('Normalized frequency'); 21 | ylabel('Gain'); 22 | axis([0 1 0 1.1]); 23 | 24 | 25 | %band pass Filter 26 | [b_pass a_pass]=butter(3,[.3 .7],'bandpass'); 27 | [H_pass,w_pass]=freqz(b_pass,a_pass); 28 | subplot(2,2,3); 29 | plot(w_pass/pi,abs(H_pass),'black','linewidth',2,'linesmoothing','on'); 30 | title('Magnitude response of a 3rd order Band pass butterworth filter with edges at .3 and .7'); 31 | xlabel('Normalized frequency'); 32 | ylabel('Gain'); 33 | axis([0 1 0 1.1]); 34 | 35 | 36 | %Band stop Filter 37 | [b_stop a_stop]=butter(3,[.3 .8],'stop'); 38 | [H_stop,w_stop]=freqz(b_stop,a_stop); 39 | subplot(2,2,4); 40 | plot(w_stop/pi,abs(H_stop),'black','linewidth',2,'linesmoothing','on'); 41 | title('Magnitude response of a 3rd order Band stop butterworth filter with band edges at .3 and .8'); 42 | xlabel('Normalized frequency'); 43 | ylabel('Gain'); 44 | axis([0 1 0 1.1]); 45 | 46 | %print('filtertypes','-dpng'); -------------------------------------------------------------------------------- /filters/filtertypes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/filtertypes.png -------------------------------------------------------------------------------- /filters/fir-filtering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/fir-filtering.png -------------------------------------------------------------------------------- /filters/fir-hamming-phase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/fir-hamming-phase.png -------------------------------------------------------------------------------- /filters/fir-hamming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/fir-hamming.png -------------------------------------------------------------------------------- /filters/forderdemonet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/forderdemonet.png -------------------------------------------------------------------------------- /filters/ideal_filter.m: -------------------------------------------------------------------------------- 1 | %ideal filters 2 | %Ideal low pass 3 | p=zeros(1,100); 4 | for t=1:100; 5 | if(t>30) 6 | p(t)=0; 7 | else 8 | p(t)=1; 9 | end; 10 | end; 11 | subplot(2,2,1) 12 | plot(p,'black','linesmoothing','on'); 13 | axis([0 100 -0.1 1.1]); 14 | xlabel('Frequency (KHz)'); 15 | ylabel('Gain') 16 | title('Ideal Low pass filter with cut off at 30 KHz') 17 | 18 | %Ideal high pass 19 | for t=1:100; 20 | if(t>40) 21 | p(t)=1; 22 | else 23 | p(t)=0; 24 | end; 25 | end; 26 | subplot(2,2,2) 27 | plot(p,'black','linesmoothing','on'); 28 | axis([0 100 -0.1 1.1]); 29 | xlabel('Frequency (KHz)'); 30 | ylabel('Gain') 31 | title('Ideal High pass filter with cut off at 40 KHz') 32 | 33 | %Ideal band pass 34 | for t=1:100; 35 | if(t>30 && t<60) 36 | p(t)=1; 37 | else 38 | p(t)=0; 39 | end; 40 | end; 41 | subplot(2,2,3) 42 | plot(p,'black','linesmoothing','on'); 43 | axis([0 100 -0.1 1.1]); 44 | xlabel('Frequency (KHz)'); 45 | ylabel('Gain') 46 | title('Ideal Band pass filter with band edges at 30 KHz and 60 KHz'); 47 | 48 | %Ideal band stop 49 | for t=1:100; 50 | if(t>30 && t<50) 51 | p(t)=0; 52 | else 53 | p(t)=1; 54 | end; 55 | end; 56 | subplot(2,2,4) 57 | plot(p,'black','linesmoothing','on'); 58 | axis([0 100 -0.1 1.1]); 59 | xlabel('Frequency (KHz)'); 60 | ylabel('Gain') 61 | title('Ideal Band stop filter with band edges at 30 KHz and 50 KHz') 62 | 63 | -------------------------------------------------------------------------------- /filters/idealfilter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/idealfilter.png -------------------------------------------------------------------------------- /filters/increasing_order.m: -------------------------------------------------------------------------------- 1 | %What happens when we increase the order of the filter. 2 | clear all; 3 | %Low pass Filter 4 | [b2 a2]=butter(2,.3,'low'); 5 | [H2,w2]=freqz(b2,a2); 6 | plot(w2/pi,abs(H2),'black','linewidth',1,'linesmoothing','on'); 7 | title('Magnitude response of a 3rd order Low pass butterworth filter with Wc at 0.3'); 8 | 9 | xlabel('Normalized frequency'); 10 | ylabel('Gain'); 11 | axis([0 1 0 1.1]); 12 | 13 | hold on; 14 | %7th order filter 15 | [b7 a7]=butter(7,.3,'low'); 16 | [H7,w7]=freqz(b7,a7); 17 | plot(w7/pi,abs(H7),'g','linewidth',1,'linesmoothing','on'); 18 | 19 | 20 | %17th order filter 21 | hold on; 22 | [b17 a17]=butter(17,.3,'low'); 23 | [H17,w17]=freqz(b17,a17); 24 | plot(w17/pi,abs(H17),'b','linewidth',1.2,'linesmoothing','on'); 25 | 26 | legend('2nd order','7th order','17th order') -------------------------------------------------------------------------------- /filters/low_elliptic.m: -------------------------------------------------------------------------------- 1 | %Low pass using elliptic filter. 2 | [N,Wn]=ellipord(0.4,0.5,0.5,60); 3 | [b,a] = ellip(N ,0.5,60,Wn); 4 | [h w]=freqz(b,a,256); 5 | H=abs(h); 6 | HdB=20*log10(H); 7 | figure (1); 8 | plot(w/pi,H,'black','linesmoothing','on'); 9 | grid on; 10 | title('Magnitude Repsonse of a Low pass Elliptic filter with Wp=0.4 and Ws=0.5 and attenuation at 60dB in stopband'); 11 | xlabel('Normailized Frequency ( rad/sec )'); 12 | ylabel('Gain'); 13 | axis([0 1 0 1.1]); 14 | print('ellip_low','-dpng'); 15 | 16 | 17 | figure (2); 18 | plot(w/pi,HdB,'black','linesmoothing','on'); 19 | grid on; 20 | title('Magnitude Repsonse of a Low pass Elliptic filter with Wp=0.4 and Ws=0.5 and ripple at 60dB in stopband'); 21 | xlabel('Normailized Frequency ( rad/sec )'); 22 | ylabel('Gain in dBs'); 23 | axis([0 1 -120 2]); 24 | 25 | print('ellip_low_dB','-dpng'); -------------------------------------------------------------------------------- /filters/oderdemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/oderdemo.png -------------------------------------------------------------------------------- /filters/signal filtering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/filters/signal filtering.png -------------------------------------------------------------------------------- /filters/sine_filter.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | f=500; 3 | fs=5e4; 4 | t=0:1/fs:5*1/f; 5 | noise=rand(size(t))/2; 6 | sine=sin(2*pi*f*t); 7 | x=sine+noise; 8 | subplot(2,2,1); 9 | hold on; 10 | plot(t,sine,'r--','linewidth',2); 11 | hold on; 12 | plot(t,noise,'c'); 13 | hold on; 14 | plot(t,x); 15 | title('Signal with Noise') 16 | ylabel('Amplitude x(t)'); 17 | xlabel('time (sec) '); 18 | legend('sine Wave','Noise','Noisy signal') 19 | 20 | n=1024; 21 | %X_noise=abs(fft(noise,n)); 22 | X=abs(fft(x,n))/300; 23 | %n=length(X); 24 | subplot(2,2,2); 25 | fVals=(0:1/(n/2-1):1); 26 | 27 | plot(fVals,X(1:n/2)); 28 | title('Signal spectrum using FFT') 29 | ylabel('Magnitude'); 30 | xlabel('Normalized Frequency (Hz) '); 31 | axis([0 .3 0 1.1]); 32 | 33 | [b a] = butter(7,.05,'low') 34 | H=freqz(b,a,floor(n/2)); 35 | hold on; 36 | plot(fVals,abs(H),'y') 37 | 38 | [b_pass a_pass]=butter(2,[0.01 0.04],'bandpass'); 39 | H_pass=freqz(b_pass,a_pass,floor(n/2)); 40 | subplot(2,2,2); 41 | plot(fVals,abs(H_pass),'r'); 42 | 43 | legend('signal','lowpass filter','bandpass filter') 44 | 45 | x_filtered=filter(b,a,x); 46 | subplot(2,2,3); 47 | plot(t,x_filtered); 48 | title('Signal filtered using low pass filter') 49 | 50 | ylabel('Amplitude x(t)'); 51 | xlabel('time (sec) '); 52 | 53 | 54 | x_passed=filter(b,a,x); 55 | subplot(2,2,4); 56 | x_adjusted=x_passed-(max(x_passed-1)); 57 | plot(t,x_adjusted); 58 | title('Signal filtered with badpass filter and adjusted for deviation') 59 | ylabel('Amplitude x(t)'); 60 | xlabel('time (sec) '); -------------------------------------------------------------------------------- /fourier_demo.m: -------------------------------------------------------------------------------- 1 | t=0:0.01:1; 2 | y=sin(2*pi*7*t)+sin(2*pi*10*t)+sin(2*pi*5*t); 3 | subplot(2,1,1); 4 | plot(t,y,'g'); 5 | title('Signal Sum of sine waves of different frequencies') 6 | 7 | f=abs(fft(y,10)); 8 | subplot(2,1,2); 9 | plot(t,f,'g'); 10 | title('Fourier transform of Sum of sine waves of different frequencies') 11 | -------------------------------------------------------------------------------- /hamming_low_fir.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | fs=2000; 3 | f1=10; 4 | f2=300; 5 | t=0:1/fs:0.1; 6 | yo=sin(2*pi*f1*t) 7 | yn=2*sin(2*pi*f2*t) 8 | y=(yo+yn)/3; 9 | figure(1); 10 | 11 | subplot(2,2,1) 12 | plot(t,yo,'black','linesmoothing','on'); 13 | title('Original Signal '); 14 | ylabel('Amplitude') 15 | xlabel('time'); 16 | 17 | subplot(2,2,2) 18 | plot(t,yn,'black','linesmoothing','on'); 19 | title('Signal To be Mixed.'); 20 | ylabel('Amplitude') 21 | xlabel('time'); 22 | 23 | 24 | subplot(2,2,3) 25 | plot(t,y,'black','linesmoothing','on'); 26 | title('Mixed Signal'); 27 | ylabel('Amplitude') 28 | xlabel('time'); 29 | 30 | 31 | N=1024; 32 | Y=abs(fft(y,N)); 33 | fVals=(0:N/2-1)*fs/N; 34 | figure(2); 35 | plot(fVals,Y(1:N/2)/max(Y),'black','linesmoothing','on'); 36 | title('Spectral Analysis of Signal sin2\pi10t mixed with sin2\pi300t'); 37 | ylabel('Magnitude') 38 | xlabel('Frequency (Hz)'); 39 | 40 | b=fir1(21,15/fs); 41 | 42 | 43 | figure(1); 44 | filtered=filter(b,1,y); 45 | subplot(2,2,4) 46 | plot(t,filtered,'black','linesmoothing','on'); 47 | title('Filtered Signal Using 21 order low pass FIR filter with Hamming window'); 48 | ylabel('Amplitude') 49 | xlabel('time'); 50 | 51 | 52 | 53 | fvtool(b); -------------------------------------------------------------------------------- /low-FIR-hamming.m: -------------------------------------------------------------------------------- 1 | b=fir1(21,0.3); 2 | plot(b); -------------------------------------------------------------------------------- /sampling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/sampling.png -------------------------------------------------------------------------------- /sampling_sine.m: -------------------------------------------------------------------------------- 1 | f=1000; 2 | %signal frequency 3 | fs=5e5; 4 | %Initially oversampled to produce a smooth plot 5 | nCyl=5; 6 | t=0:1/fs:nCyl*1/f; 7 | y=sin(2*pi*f*t); 8 | subplot(3,1,1); 9 | plot(t,y,'b'); 10 | title('Continuous sinusoidal signal of frequency=1000Hz'); 11 | xlabel('Time(sec)'); 12 | ylabel('Amplitude Y(t)'); 13 | axis([0 nCyl/f -1.1 1.1]) 14 | 15 | %Undersampling 16 | fs1=1500; 17 | t1=0:1/fs1:nCyl*1/f; 18 | y1=sin(2*pi*f*t1); 19 | 20 | %OverSampling 21 | fs2=4000; 22 | t2=0:1/fs2:nCyl*1/f; 23 | y2=sin(2*pi*f*t2); 24 | 25 | subplot(3,1,2) 26 | plot(t,y,'r'); 27 | hold on; 28 | plot(t1,y1,':'); 29 | hold on; 30 | stem(t1,y1,'b','filled') 31 | title('Continuous sinusoidal signal sampled at 1500Hz < Nq i.e. 2000Hz (If you look carefully there seem 2.5 cycles instead of 5) '); 32 | xlabel('Time(sec)'); 33 | ylabel('Amplitude Y(t)'); 34 | axis([0 nCyl/f -1.1 1.1]) 35 | 36 | subplot(3,1,3) 37 | plot(t,y,'r'); 38 | hold on; 39 | plot(t2,y2,':'); 40 | hold on; 41 | stem(t2,y2,'b','filled') 42 | title('Continuous sinusoidal signal sampled at 4000Hz > Nq '); 43 | xlabel('Time(sec)'); 44 | ylabel('Amplitude Y(t)'); 45 | axis([0 nCyl/f -1.1 1.1]); 46 | 47 | print('sampling','-dpng'); %Use this to print the png of the graph plotted. -------------------------------------------------------------------------------- /sinefft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/sinefft.png -------------------------------------------------------------------------------- /sinefourier.m: -------------------------------------------------------------------------------- 1 | f1=10; 2 | f2=15; 3 | sampleFactor=30; 4 | fs=sampleFactor*f; 5 | phase=pi/3; 6 | nCyl=5; 7 | t=0:1/fs:1; 8 | 9 | x=sin(2*pi*f1*t +phase)+sin(2*pi*f2*t); 10 | subplot(2,2,1); 11 | plot(t,x,'LineSmoothing','on'); 12 | title('X(t) is sin(2\pi15t) + sin(2\pi10t) '); 13 | xlabel('time (sec)'); 14 | ylabel(' Amplitude X(t)'); 15 | mx=max(x); 16 | axis([0 1 -1.2*mx 1.2*mx]); 17 | 18 | NFFT=1024; 19 | X=fftshift(fft(x,NFFT)); 20 | fVals=fs*(-NFFT/2:NFFT/2-1)/NFFT; 21 | subplot(2,2,2); 22 | plot(fVals,X,'LineSmoothing','on','LineWidth',1); 23 | title('Double Sided FFT with FFTshift'); 24 | ylabel('|DFT values|'); 25 | xlabel('Frequency (Hz)'); 26 | f=max(f1,f2)+10; 27 | mX=1.1*abs(max(X)); 28 | axis([-f f -10 mX]); 29 | 30 | L=length(x); 31 | Px=X.*conj(X)/(NFFT*L); 32 | subplot(2,2,3); 33 | plot(fVals,Px,'LineSmoothing','on','LineWidth',1); 34 | title('Power Spectral Density'); 35 | ylabel('Power'); 36 | xlabel('Frequency (Hz)'); 37 | 38 | axis([-f f 0 mp]); 39 | 40 | 41 | 42 | sVals=fs*(0:NFFT/2-1)/NFFT; 43 | subplot(2,2,4); 44 | plot(fVals,Px,'LineSmoothing','on','LineWidth',1); 45 | title('One Sided Power Spectral Density'); 46 | ylabel('Power spectral density'); 47 | xlabel('Frequency (Hz)'); 48 | axis([0 f 0 mp]); 49 | grid on; 50 | print('sinefft','-dpng'); 51 | %http://www.gaussianwaves.com/2014/07/how-to-plot-fft-using-matlab-fft-of-b 52 | %asic-signals-sine-and-cosine-waves/ -------------------------------------------------------------------------------- /sources.txt: -------------------------------------------------------------------------------- 1 | https://dadorran.wordpress.com/2013/10/18/filter-design-using-matlab-demo/ 2 | https://www.diva-portal.org/smash/get/diva2:630301/FULLTEXT02.pdf 3 | http://www.gaussianwaves.com/category/signal-processing/ 4 | http://www.phys.nsu.ru/cherk/fft.pdf -------------------------------------------------------------------------------- /zdemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitijpurwar/DSP_using_matlab/8fde30fcda5600c0226d5b3b1b802d657b2e6288/zdemo.png --------------------------------------------------------------------------------