Advertisement
defineSN

sampling DSP equations using multi-dim. cell array in MATLAB

Apr 8th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.13 KB | None | 0 0
  1. %6.FUNCTION SAMPLING PROGRAM: SINGLE & MULTIPLE (SUMMED) FUNCTION'S
  2. %SAMPLING
  3. disp('ADDITION/SAMPLING OF WAVES PROGRAM')
  4. N=input('enter the number upto which sampling will occur:\n');
  5. ss=input('enter desired sample spacing\n');
  6. n=0:ss:N-1; % sampling step
  7. tn=N/ss; % this is the total number of samples
  8. p=input('number of individual equations:\n');
  9.    eq_str{p,3}='setup' % matlab showed an error that eq_str() 's size is contanly increasing, preallocate for speed. hence this line.
  10. for row=1:p
  11.    fprintf('for equation number: %d \n',row)
  12.    eq_data(row,:)=input('enter the equation:\n'); % data stored as a <p x tn> double
  13.    eq_str{row,1}=input('re-enter equation[this is for title]:\n','s');  % stored as a <3x3> cell
  14. end
  15. % eq_data is an array that stores p no. of equations, each with n samples,
  16. % the samples will be stored in the columns of this 2-D array
  17. % eq_str stores the text input for display in the title of the plot
  18.  
  19.  
  20. for row=1:p-1
  21.  % this adds '+' to 2nd column of all rows
  22.  eq_str{row,2}='+';
  23. end
  24. for row=1:p
  25.    eq_str{row,3}=strcat(eq_str{row,1},eq_str{row,2});
  26.    % concatenates 1st and 2nd columns and stores it in the 3rd column
  27.    % concatenation with strcat() eliminates any trailing spaces, to keep trailing spaces, use horzcat()
  28.    % see strcat() in help menu for further details
  29. end
  30.  
  31. eq_str  
  32. T=strcat(eq_str{:,3})
  33. % concatenates the 3rd column and strores it as a string in T.
  34. % nb: eq_str(:,3) will result in error, it is used only for displaying purposes,
  35. % to edit data, {} has to be used instead of () as i'v used here
  36.  
  37. if(p>1)
  38.     y=sum(eq_data);
  39. elseif(p==1)
  40.     % for a single equation, sum(eq_data) will add all the samples and generate a number, stem() cannot plot a number, hence this line
  41.     y=eq_data;
  42. end
  43.  
  44. stem(n,y);
  45. grid;
  46. xlabel('Number of Samples');
  47. ylabel('Amplitude');
  48. title(['Addition/Sampling of waves: ',T]);
  49. clearvars eq_data eq_str;
  50.  
  51. % memory cleared as when a different number of samples is
  52. % desired, an array out of bounds type of error will generate, as
  53. % eq_data() is a array whose columns have been fixed to the number of
  54. % samples on the 1st run of this code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement