Advertisement
redsees

Untitled

Mar 25th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.52 KB | None | 0 0
  1. function x = gen_fn(c)
  2.     global sp ep sf bp t;
  3.     %t=linspace(sp,ep,sf*(ep-sp)+1);
  4.     clc;
  5.     fprintf('Do you want to plot a continuous or a discrete signal ?\n');
  6.     cd=lower(input('Your Choice : ','s'));
  7.     if not(strcmp(cd,'continuous') || strcmp(cd,'discrete'))
  8.         fprintf('Invalid Input !\n\n');
  9.         x=-1;
  10.         return;
  11.     end
  12.  
  13.     switch c
  14.         case 1
  15.             %Here comes the code for Sinusoidal Plotting .
  16.             %f=A*sin(2*pi*f*t+(ph*pi/180))
  17.             fprintf('\nYour choice was Sinusoidal !\n\n');
  18.             if bp
  19.                 sep=str2num(input('Enter this function''s end point ','s'));
  20.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  21.                 bp=bp-1;
  22.                 sp=sep;
  23.             end
  24.             A=str2num(input('Amplitude : ','s'));
  25.             ps=str2num(input('Phase Shift in degrees : ','s'));
  26.             fq=str2num(input('Frequency : ','s'));
  27.             f=A*sin(2*pi*fq*t+(ps*pi)/180);
  28.             if strcmp(cd,'continuous')
  29.                 plot(t,f,'r');
  30.             else
  31.                 stem(t,f,'r');
  32.             end
  33.             hold on;
  34.         case 2
  35.             %Here comes the code for DC Plotting .
  36.            fprintf('\nYour choice was DC !\n\n');
  37.             if bp
  38.                 sep=str2num(input('Enter this function''s end point ','s'));
  39.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  40.                 bp=bp-1;
  41.                 sp=sep;
  42.             end
  43.             dcv=str2num(input('DC value : ','s'));
  44.             x=linspace(sp,ep,sf*(ep-sp)+1);
  45.             y=x*0;
  46.             z=0;
  47.             for i=linspace(sp,ep,sf*(ep-sp)+1);
  48.                 z=z+1;
  49.                 if (i==0) y(z:length(y))=1;break; end
  50.             end
  51.             if strcmp(cd,'continuous') plot(x,dcv*y);
  52.             else stem(x,dcv*y); end
  53.             axis();
  54.             xlabel('t');
  55.             ylabel('DC Function');
  56.             hold on;
  57.         case 3
  58.             %Here comes the code for Ramp Plotting .
  59.             fprintf('\nYour choice is Ramp !\n\n');
  60.             if bp
  61.                 sep=str2num(input('Enter this function''s end point ','s'));
  62.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  63.                 bp=bp-1;
  64.                 sp=sep;
  65.             end
  66.             slope=str2num(input('Ramp slope : ','s'));
  67.             intrspt=str2num(input('Interception with Y-axix : ','s'));
  68.             if strcmp(cd,'continuous') plot(t,slope*t+intrspt);
  69.             else stem(t,slope*t+intrspt); end
  70.             xlabel('t');
  71.             ylabel('Ramp function');
  72.             hold on;
  73.         case 4
  74.             %Here comes the code for Exponential Plotting .
  75.             fprintf('\nYour choice was Exponential !\n\n');
  76.             if bp
  77.                 sep=str2num(input('Enter this function''s end point ','s'));
  78.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  79.                 bp=bp-1;
  80.                 sp=sep;
  81.             end
  82.             A=str2num(input('Amplitude : ','s'));
  83.             ec=str2num(input('Exponential constant : ','s'));
  84.             f=exp(ec*t);
  85.             if strcmp(cd,'continuous')
  86.                 plot(t,f,'b');
  87.             else
  88.                 stem(t,f,'b');
  89.             end
  90.             hold on;
  91.         case 5
  92.             %Here comes the code for impulse Plotting .
  93.             fprintf('\nYour choice was Impulse !\n\n');
  94.             if bp
  95.                 sep=str2num(input('Enter this function''s end point ','s'));
  96.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  97.                 bp=bp-1;
  98.                 sp=sep;
  99.             end
  100.             loc=str2num(input('Impulse location : ','s'));
  101.             if loc>ep||loc<sp
  102.                 fprintf('Wrond entry !');
  103.                 x=-1;return;
  104.             end
  105.             for i = 1 : length(t)
  106.                 if (t(i)==loc) x(i)=1;
  107.                 else x(i)=0;
  108.                 end
  109.             end
  110.             if strcmp(cd,'continuous')
  111.                 plot(t,x);
  112.             else
  113.                 stem(t,x);
  114.             end
  115.             axis();
  116.             xlabel('n');
  117.             ylabel('Impulse Function');
  118.             hold on;
  119.         case 6
  120.             %Here comes the code for Unit Step Plotting .
  121.             fprintf('Your choice is Unit step Function\n\n');
  122.             if bp
  123.                 sep=str2num(input('Enter this function''s end point ','s'));
  124.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  125.                 bp=bp-1;
  126.                 sp=sep;
  127.             end
  128.             amp=str2num(input('Amplitude : ','s'));
  129.             tl=str2num(input('Location of transition : ','s'));
  130.             x=linspace(sp,ep,sf*(ep-sp)+1);
  131.             y=x*0;
  132.             z=0;
  133.             for i=linspace(sp,ep,sf*(ep-sp)+1);
  134.                 z=z+1;
  135.                 if (i==tl) y(z:length(y))=1;break; end
  136.             end
  137.             if strcmp(cd,'continuous') plot(x,amp*y);
  138.             else stem(x,amp*y); end
  139.             axis();
  140.             xlabel('t');
  141.             ylabel('Unit Step Function');
  142.             hold on;
  143.         case 7
  144.             %Here comes the code for Rectangular Plotting .
  145.             fprintf('\nYour choice was Rectangular !\n\n');
  146.             if bp
  147.                 sep=str2num(input('Enter this function''s end point ','s'));
  148.                 t=linspace(sp,sep,sf*(sep-sp)+1);
  149.                 bp=bp-1;
  150.                 sp=sep;
  151.             end
  152.             wids=(str2num(input('Rectangle Width : ','s'))/2);
  153.             amp=str2num(input('Rectangle Amplitude : ','s'));
  154.             center=str2num(input('Rectangle Center : ','s'));
  155.             x=linspace(sp,ep,sf*(ep-sp)+1);
  156.             %y=not((abs(x) <= (center-wids)) | ( abs(x) >= (center+wids)));
  157.             y=not((x <= (center-wids)) | ( x >= (center+wids)));
  158.             y=y*amp;
  159.             if strcmp(cd,'continuous') plot(x,y);
  160.             else stem(x,y); end
  161.             xlabel('t');
  162.             ylabel('Rectangular Function');
  163.             hold on;
  164.             %x=1;syms x;
  165.             %if strcmp(cd,'continuous')
  166.             %ezplot(A*rectangularPulse(-wid,wid,x-cor),[sp ep]);
  167.         otherwise
  168.             disp('Nothing to be done !')
  169.     end
  170.     hold on;
  171.     x=0;
  172. end
  173. %HAMID'S FUNCTIONS :
  174. %%%%%%%%%%%%%%%%%%%%
  175. %fprintf('Your choice is Unit step Function\n\n');
  176. %
  177. %start_point=input('The start point is = ');
  178. %end_point=input('The end point is = ');
  179. %%%smpl_freq=input('The sampling frequancy is = ');
  180. %amp=input('The Amplitude is :');
  181. %trans_location=input('The location of transition is = ');
  182. %
  183. %t=[trans_location-abs(start_point):trans_location trans_location:abs(end_point)+trans_location] %%el zero mara bib2a sorto zero, w mara bib2a sorto 1 3shan nozbot el rasma fel plot
  184. %q=[zeros(1,abs(start_point)+1) ones(1,abs(end_point)+1)]
  185. %plot (t,amp*q);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement