Matlab - session #4
Programming exercises
In today's session, we will tackle two programming exercises. In each case, our task is to find the average peak values of several waveforms. In the first example, 10 waveforms of different amplitudes are synchronized in time. In the second case, the waveforms occur spontaneously and asynchronously during a single sweep.

Exercise 1: Find the average of the peak values of the waveforms shown in the figure below.

Run the function named 'mat01b', which generates the figure below.
The function solves the equation y=x*exp(-x) for x from 0 to 9, scales it to different amplitudes 10 times, and plots the 10 scaled waveforms. The 10 sweeps are in the array named 'y' - it contains 10 columns (1 column per sweep) and 91 rows.

Now open 'mat01b' in the Matlab editor (type 'edit mat01b' (without the quotes) in the Command window). Add code to the end of the function that calculates (and displays) the average of all of the peak values. Remember: the data are stored in an array named 'y' - one column per waveform, with 10 waveforms altogether. Try to do it without looking at the answer (below).
Add your code, save the file, and then run it. If it doesn't work, it is the Instructor's fault.



The following code ('mat01b.m') generates the waveforms to the right. Data are stored in the array named 'y'.
  • function mat01b(varargin)
  • global v vh
  • close all
  • x=[0:0.1:9]; % make array 'x' - from 0 to 9 in steps of 0.1
  • y0=x.*exp(-x);
  • y0=y0./max(y0(:)); % set to max=1.0
  • y0=y0'; % switch row to column
  • sweeps=10;
  • y=zeros(size(x,2),sweeps); % initialize y
  • for j=1:sweeps
  • y(:,j)=rand*y0; % add a new column
  • end
  • plot(y,'-o')
  • % PUT CODE HERE TO DETERMINE AVERAGE PEAK VALUE


    Exercise 2. Find the average of the peak amplitudes of the spontaneous events in the figure below.

    Run the function 'mat01c', which generates the figure below.
    This function, like 'mat01b' above, solves y=x*exp(-x), but instead of putting the results into a two-dimensional array, it just concatenates the waveforms in one row. In addition, it creates some 'jitter' in the timing, so the peaks do not occur at any fixed interval.

    Now edit 'mat01c'. Add code to the end of the function that calculates (and displays) the average of all of the peak values. Remember: the data are stored in an array named 'y' - which contains one row, with 10 waveforms strung out in a series.


    function mat01c(varargin)
  • global v vh
  • close all
  • x=[0:0.1:20]; % make array 'x' longer than in the previous example
  • y0=x.*exp(-x);
  • y0=y0./max(y0(:)); % scale to max of 1.0
  • sweeps=10;
  • y=[]; % initialize y
  • for j=1:sweeps
  • yy=rand*y0(1:end-round(150*rand)); % variable length (subtract up to 150 points from end of array)
  • y=[y yy]; % concatenate with others
  • end
  • line('xdata',[1:size(y,2)],'ydata',y)
  • % PUT NEW CODE HERE TO FIND AVERAGE OF PEAK VALUES



    ANSWERS

    Exercise 1. Run the file named 'mat01b_answer' to see the results (pictured below).

    This code, added to the end of mat01b, finds the average of all peaks, draws a horizontal line at this value, and labels the line with the average value.
  • mx=max(y); % mx has 10 answers (1 per column)
  • avg=sum(mx)/sweeps; % sum(mx) gives 1 answer, then divide by number of waveforms
  • line('xdata',[0 size(y,1)],'ydata',[avg avg])
  • text('position',[size(x,2)/2,avg],...
    'string',['Avg=' num2str(avg)],...
    'verticalalignment','bottom')




    Exercise 2. Run the file named 'mat01c_answer' to see the results (pictured below).

    This code, added to mat01c, finds and marks the peak values and displays the average of all peak values.

  • hline=line('xdata',[1:size(y,2)],...
    'ydata',y,'color','red'); % line to show changes
  • thresh=0.03; % threshold for detection of peaks
  • prepts=25; postpts=60; % # points around peak to be zeroed after peak detection
  • OK=1; % keep searching for max
  • mxtot=0; nn=0; % summary (sum total and number) of all detected, to calculate average
  • while OK % keep going until no values exceed threshold
  • mx=max(y(:)); % find max value of entire array
  • if mx>thresh
  • [r,c]=find(y==mx); c=c(1); % find max position
  • mxtot=mxtot+mx; nn=nn+1; % update summary info
  • y(1,max(1,c-prepts):min(size(y,2),c+postpts))=0; % zero points around peak
  • set(hline,'ydata',y) % display changed line
  • text('position',[c mx],...
    'fontsize',24,'string','*') % mark peak with asterisk
  • pause(0.2) % pause 200 msec
  • else
  • OK=0; % no max above threshold. Time to abort.
  • end
  • end
  • avg=mxtot/nn; % average
  • line('xdata',[0 size(y,2)],'ydata',[avg avg]) % draw line at average
  • text('position',[size(y,2)/2 avg],'string',...
    ['Average=' num2str(avg)],...
    'verticalalignment','bottom') % label line with value