编程语言
首页 > 编程语言> > 第一个scilab程序

第一个scilab程序

作者:互联网

    今天已经是农历腊月二十四了,过年的脚步很近了。

    最近开始重新看书,想把以前写过的matlab代码换成scilab的,今天奉上第一个程序,以资鼓励,

    

    题目要求计算卷积。   

    scialb代码如下:

   

  1 // <<DSP using MATLAB>>3rd edition 
  2 // Book Author:Vinay K Ingle, John G Proakis
  3 //
  4 // Example 2.8
  5 // script by: KY
  6 //
  7 clear, clc, clf();
  8 
  9 mode(2);
 10 funcprot(0);
 11 exec('fun_banner.sci');          // output version and OS info
 12 exec('fun_conv_m.sci');          // 
 13 
 14 // ------------------------------------------------------------------------
 15 //            Output Info about this sce-file
 16 mprintf('\n***********************************************************\n');
 17 [ban1,ban2] = fun_banner();
 18 mprintf("     <DSP using MATLAB>3rd edition, Example 2.8  \n\n");
 19 
 20 // ------------------------------------------------------------------------
 21 
 22 // Input sequence
 23 x = [3, 11, 7, 0, -1, 4, 2]; nx = [-3:3];
 24 // Impulse Response sequence
 25 h = [2, 3, 0, -5, 2, 1]; nh = [-1:4];
 26 
 27 [y,ny] = fun_conv_m(x,nx,h,nh)
 28 
 29 f=gcf();                                   // Get figure(window) handle
 30 //  f.figure_size=[700,600];                 // adjust window size
 31 //  f.background=2;                        // add background color 8-white
 32   f.figure_name="Example 2.4";                // name window
 33    
 34 // ------------------------------------------------------------------------      
 35 // x(k) and h(k)
 36 subplot(2, 2, 1); 
 37 plot(nx,x,'bo');
 38 plot(nx,x,'b.');
 39 plot2d3(nx,x,2);                      // Create plot with blue line
 40 e=gce();
 41 e.children(1).line_style=1;
 42 //set(gca(),"auto_clear","off");      // hold on(MATLAB)
 43 plot(nh+0.1,h,'ro');
 44 plot(nh+0.1,h,'r.');
 45 plot2d3(nh+0.1,h,5);                  // Create plot with blue line
 46 title('x(k) and h(k)');
 47 xlabel('k');
 48 xgrid();                              // grid with black color
 49 a=get("current_axes");                // get the handle of the newly created axes
 50 a.data_bounds=[-5,5,-6,13];
 51 e=gce();
 52 e.children(1).line_style=2;
 53 xstring(-1.5,11,['Blue :x  Red :h']);   // matrix contain the string of the object
 54 //set(gca(),"auto_clear","on");         // hold off(MATLAB)
 55 
 56 
 57 // -------------------------------------------------------------------------
 58 // x(k) and h(-k)
 59 subplot(2, 2, 2); 
 60 plot(nx,x,'bo');                        // x(k)
 61 plot(nx,x,'b.');
 62 plot2d3(nx,x,2);                      // Create plot with blue line
 63 e=gce();
 64 e.children(1).line_style=3;
 65 plot(-flipdim(nh,2)+0.1,flipdim(h,2),'ro');     // h(-k)
 66 plot(-flipdim(nh,2)+0.1,flipdim(h,2),'r.');
 67 plot2d3(-flipdim(nh,2)+0.1,flipdim(h,2),5);     // Create plot with blue line
 68 title('x(k) and h(-k)');
 69 xlabel('k');
 70 xgrid();                            // grid with black color
 71 a=get("current_axes");            //get the handle of the newly created axes
 72 a.data_bounds=[-5,5,-6,13];
 73 e=gce();
 74 e.children(1).line_style=4;
 75 //e.children(2).polyline_style=2;
 76 xstring(-1.5,11,['Blue :x  Red :h']);   // matrix contain the string of the object
 77 xstring(-0.5, -1, 'n=0');
 78  
 79  
 80 // --------------------------------------------------------------------------
 81 // x(k) and h(-1-k)
 82 subplot(2, 2, 3); 
 83 plot(nx,x,'bo');                                  // x(k)
 84 plot(nx,x,'b.');
 85 plot2d3(nx,x,2);                                // Create plot with blue line
 86 e=gce();
 87 e.children(1).line_style=5;
 88 plot(-flipdim(nh,2)+0.1-1,flipdim(h,2),'ro');     // h(-1-k)
 89 plot(-flipdim(nh,2)+0.1-1,flipdim(h,2),'r.');
 90 plot2d3(-flipdim(nh,2)+0.1-1,flipdim(h,2),5);     // Create plot with blue line
 91 title('x(k) and h(-1-k)');
 92 xlabel('k');
 93 xgrid();                            // grid with black color
 94 a=get("current_axes");            //get the handle of the newly created axes
 95 a.data_bounds=[-5,5,-6,13];
 96 e=gce();
 97 e.children(1).line_style=6;
 98 //e.children(6).polyline_style=6;
 99 xstring(-1.5,11,['Blue :x  Red :h']);   // matrix contain the string of the object
100 xstring(-1-0.5, -1, 'n=-1');
101  
102 
103 // ----------------------------------------------------------------------------
104 // x(k) and h(2-k)
105 subplot(2, 2, 4); 
106 plot(nx,x,'bo');                                  // x(k)
107 plot(nx,x,'b.');
108 plot2d3(nx,x,2);                                // Create plot with blue line
109 e=gce();
110 e.children(1).line_style=9;
111 plot(-flipdim(nh,2)+0.1+2,flipdim(h,2),'ro');     // h(2-k)
112 plot(-flipdim(nh,2)+0.1+2,flipdim(h,2),'r.');
113 plot2d3(-flipdim(nh,2)+0.1+2,flipdim(h,2),5);     // Create plot with blue line
114 title('x(k) and h(2-k)');
115 xlabel('k');
116 xgrid();                            // grid with black color
117 a=get("current_axes");            //get the handle of the newly created axes
118 a.data_bounds=[-5,5,-6,13];
119 e=gce();
120 e.children(1).line_style=10;
121 //e.children(3).polyline_style=8;
122 xstring(-1.5,11,['Blue :x  Red :h']);   // matrix contain the string of the object
123 xstring(2-0.5, -1, 'n=2');
View Code

  运行结果:

  

     学习scilab三个月来初步体会:

     1、又多了一个研究问题的工具。听说有些地方初高中课本中已经介绍了scilab,如果将来辅导孩儿作业,那么现在就可以提前做些准备了;

     2、帮助手册写的还是比较详细的,虽然是英文,只要过了四级,一般都能看懂,极个别的单词找百度。其中有个章节映像很深,写的是

matlab和scilab有关命令的对照关系,这个太方便了,可以参照它就能把原来的matlab代码手工转换成scilab。主要是自带的转换,我还不太会用,

就先一步一步的慢慢手动转吧。

 

标签:nh,plot,第一个,0.1,程序,nx,flipdim,line,scilab
来源: https://www.cnblogs.com/ky027wh-sx/p/15847875.html