Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %6.FUNCTION SAMPLING PROGRAM: SINGLE & MULTIPLE (SUMMED) FUNCTION'S
- %SAMPLING
- disp('ADDITION/SAMPLING OF WAVES PROGRAM')
- N=input('enter the number upto which sampling will occur:\n');
- ss=input('enter desired sample spacing\n');
- n=0:ss:N-1; % sampling step
- tn=N/ss; % this is the total number of samples
- p=input('number of individual equations:\n');
- eq_str{p,3}='setup' % matlab showed an error that eq_str() 's size is contanly increasing, preallocate for speed. hence this line.
- for row=1:p
- fprintf('for equation number: %d \n',row)
- eq_data(row,:)=input('enter the equation:\n'); % data stored as a <p x tn> double
- eq_str{row,1}=input('re-enter equation[this is for title]:\n','s'); % stored as a <3x3> cell
- end
- % eq_data is an array that stores p no. of equations, each with n samples,
- % the samples will be stored in the columns of this 2-D array
- % eq_str stores the text input for display in the title of the plot
- for row=1:p-1
- % this adds '+' to 2nd column of all rows
- eq_str{row,2}='+';
- end
- for row=1:p
- eq_str{row,3}=strcat(eq_str{row,1},eq_str{row,2});
- % concatenates 1st and 2nd columns and stores it in the 3rd column
- % concatenation with strcat() eliminates any trailing spaces, to keep trailing spaces, use horzcat()
- % see strcat() in help menu for further details
- end
- eq_str
- T=strcat(eq_str{:,3})
- % concatenates the 3rd column and strores it as a string in T.
- % nb: eq_str(:,3) will result in error, it is used only for displaying purposes,
- % to edit data, {} has to be used instead of () as i'v used here
- if(p>1)
- y=sum(eq_data);
- elseif(p==1)
- % for a single equation, sum(eq_data) will add all the samples and generate a number, stem() cannot plot a number, hence this line
- y=eq_data;
- end
- stem(n,y);
- grid;
- xlabel('Number of Samples');
- ylabel('Amplitude');
- title(['Addition/Sampling of waves: ',T]);
- clearvars eq_data eq_str;
- % memory cleared as when a different number of samples is
- % desired, an array out of bounds type of error will generate, as
- % eq_data() is a array whose columns have been fixed to the number of
- % samples on the 1st run of this code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement