Advertisement
MK7265

Untitled

Mar 9th, 2023 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 3.78 KB | None | 0 0
  1. //Practical 1 (Generation of Discrete Signals)
  2.  
  3. //Unit Impluse Signal (1,for n=0 or 0,otherwise)
  4. y1 = zeros(1,21);
  5. y1(1,11) = 1;
  6. subplot(3,2,1);
  7. plot2d3(y1);
  8. title("Unit Impluse Signal");
  9.  
  10. //Unit Step Signal (1, for n>=0 or 0, for n<0)
  11. y2 = zeros(1,21);
  12. y2(1,11:21) = 1;
  13. subplot(3,2,2);
  14. plot2d3(y2);
  15. title("Unit Step Signal");
  16.  
  17. //Unit Ramp Signal (n,for n>=0 or 0,for n<0)
  18. y3=0:10;
  19. subplot(3,2,3);
  20. plot2d3(y3);
  21. title("Unit Ramp Signal");
  22.  
  23. //Exponential Signal
  24.  
  25. //Increasing
  26. t = -2:0.1:2;
  27. x = exp(t);
  28. subplot(3,2,4);
  29. plot(t,x);
  30. title("Exponential Increasing");
  31.  
  32. //Decreasing
  33. t = -2:0.1:2;
  34. x = exp(-t);
  35. subplot(3,2,5);
  36. plot(t,x);
  37. title("Exponential Decreasing");
  38.  
  39.  
  40. //Practical 2 (Linear Convolution of two sequence)
  41.  
  42. //Linear Convolution of two sequence
  43. x = [1,2,3,4];
  44. h = [1,1,1];
  45. l1 = length (x);
  46. l2 = length (h);
  47. L = l1+l2-1;
  48. x = [x,zeros(1,L-l1)];
  49. h = [h,zeros(1,L-l2)];
  50. y = zeros(1,L);
  51. for n=1:L;
  52.     for k=1:n
  53.         y(n) = y(n)+x(k)*h(n-k+1);
  54.     end
  55. end
  56. disp(y);
  57. //Plotting
  58. ny = 0:L-1;
  59. subplot(3,1,1);
  60. plot2d3(ny,x);
  61. title("For x");
  62. subplot(3,1,2);
  63. plot2d3(ny,h);
  64. title("For h");
  65. subplot(3,1,3);
  66. plot2d3(ny,y);
  67. title("For y");
  68.  
  69. //Practical 3 (Circular Convolution of two Sequence)
  70.  
  71. //Circular Convolution of two sequence
  72. xn = [1,2,3,4];
  73. xn = mtlb_fliplr(xn);
  74. hn = [2,1,2,1];
  75. x1 = xn;
  76. h1 = hn;
  77. for i=1:length(xn);
  78.     x1=[x1($)x1(1:$-1)];
  79.     h1 = hn;
  80.     ycc(i) = sum(x1.*h1);
  81.     disp(ycc(i));
  82. end
  83.  
  84. //Practical 4 (Perform Circular Convolution using DFT and IDFT)
  85.  
  86. x1 = input("Enter Input Sequence for x1(n) : ");
  87. x2 = input("Enter Input Sequence for x2(n) : ");
  88. l1 = length(x1);
  89. l2 = length(x2);
  90. if l1 == l2 then
  91.     a=fft(x1,-1);
  92.     b=fft(x2,-1);
  93.     x3 = a.*b;
  94.     disp(x3);
  95.     x4=ifft(x3);
  96.     disp(x4);
  97. elseif l1<l2
  98.     x1 = [x1,zeros(1,l2-l1)];
  99.     a=fft(x1,-1);
  100.     b=fft(x2,-1);
  101.     x3 = a.*b;
  102.     disp(x3);
  103.     x4=ifft(x3);
  104.     disp(x4);
  105. else
  106.     x2 = [x2,zeros(1,l1-l2)];
  107.     a=fft(x1,-1);
  108.     b=fft(x2,-1);
  109.     x3 = a.*b;
  110.     disp(x3);
  111.     x4=ifft(x3);
  112.     disp(x4);
  113. end
  114.  
  115.  
  116. //Practical 5 ( Perform Linear convolution using circular convolution method)
  117.  
  118. //Linear Convolution method using circular convolution method
  119. xn = [1,2,3,1,0,0];
  120. hn = [1,1,1,0,0,0];
  121. xn = mtlb_fliplr(xn);
  122. x1 = xn;
  123. h1 = hn;
  124. for i=1:length(xn);
  125.     x1 = [x1($)x1(1:$-1)];
  126.     h1 = hn;
  127.     ycc(i) = sum(x1.*h1);
  128.     disp(ycc(i));
  129. end
  130.  
  131. //Practical 6 (Perform FFT and IFFT of a discrete sequence)
  132.  
  133. //DSP Practical 6 (Butterfly Method)
  134. clear;
  135. x = [1,1,0,0,1,1,0,0];
  136. n = [0,4,2,6,1,5,3,7]+1;
  137. for i=1:8
  138.     s=n(i);
  139.     xn(i)=x(s);
  140. end
  141.  
  142. //Stages
  143. O = xn;
  144. N = length(x);
  145. S = log2(N);
  146. cnt=1;
  147.  
  148. //Stage 1
  149. for n=1:2:N-1
  150.     w8_0=exp(-1*%i*2*%pi*0/N);
  151.     f(cnt)=O(n)+O(n+1)*w8_0;
  152.     f(cnt+1)=O(n)-O(n+1)*w8_0;
  153.     cnt=cnt+2;    
  154. end
  155. disp(f)
  156.  
  157. //Stage 2
  158. cnt=1;
  159. for n=1:4:N-1
  160.     w8_0=exp(-1*%i*2*%pi*0/N);
  161.     w8_2=exp(-1*%i*2*%pi*2/N);
  162.     G(cnt)=f(n)+f(n+2)*w8_0;
  163.     G(cnt+2)=f(n)-f(n+2)*w8_0;
  164.     G(cnt+1)=f(n+1)+f(n+3)*w8_2;
  165.     G(cnt+3)=f(n+1)-f(n+3)*w8_2;
  166.     cnt=cnt+4;
  167. end
  168. disp(G)
  169.  
  170. //Stage 3
  171. cnt=1;
  172. for n = 1:N/2
  173.     w8 = exp(-1*%i*2*%pi/N);
  174.     X(cnt) = G(n)+G(n+4)*w8^(n-1);
  175.     X(cnt+4) = G(n)-G(n+4)*w8^(n-1);
  176.     cnt = cnt+1;
  177. end
  178. disp(X)
  179.  
  180.  
  181. //Practical 7 (To design a FIR filter using window technique)
  182.  
  183. //DSP Practical 7 (Rectangle Window)
  184. clc;clear;
  185. tmin = 0;tmax = 1;
  186. t = linspace(tmin,tmax,256);
  187. [fir_time,fir_freq,fr] = wfir("lp",133,[.2,0],"re",[0,0]);
  188. subplot(2,2,1);
  189. title('h(n)');
  190. plot(fir_time);
  191.  
  192. subplot(2,2,2);
  193. title('H(W)');
  194. plot(fir_freq);
  195.  
  196. X = sin(2*%pi*t*64)+sin(2*%pi*t*128)+sin(2*%pi*t*256);
  197. subplot(2,2,3);
  198. Y = fft(X);
  199. title('Signal IFFT');
  200.  
  201. plot(abs(Y));
  202. subplot(2,2,4);
  203.  
  204. YF = abs(Y).*fir_freq;
  205. title('FIR Filtered Signal');
  206. plot(abs(YF));
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement