% ECE 1250 Lecture 6 %------------------------------------------------------------------------------- % Array Processing, Fourier Series, and Plot Labeling %------------------------------------------------------------------------------- % Fourier theory says any waveform may be approximated as a sum of % sinusoids of different frequencies, amplitudes, and phase shifts. % Ex: Fourier series for square wave, first three terms. % First we create a vector of time values. >> t=(1:1000)/8000; % Divide by 8000 to match sampling rate for sounds % Set the frequency of the square wave we want to approximate at 100 Hz. >> sqfreq = 100; % Create sinusoids at frequencies of 100 Hz, 300 Hz, and 500 Hz. % Note that f1, f3, and f5 are arrays of 1000 points each. >> f1 = cos(2*pi*sqfreq*t); >> f3 = (-1/3)*cos(2*pi*3*sqfreq*t); >> f5 = (1/5)*cos(2*pi*5*sqfreq*t); % Sum the three sinusoids. >> y = f1 + f3 + f5; % Start a new figure. >> figure % Use a bigger font than normal for labels. >> axes('FontSize',14,'Parent',figure) % Must be done before plot. % Plot the y waveform as red points. % 'r.' is a string that specifies red and dots) >> plot(t,y,'r.') % We get an approximation of a square wave. % Add some nice labels. >> xlabel('Time') >> ylabel('Voltage') >> title('Fourier Series for Square Wave') % Set the endpoints of the axes >> axis([0, 1000/8, -2, 2]) % Format is [xmin, xmax, ymin, ymax] % It sounds kind of grungy >> sound(y) %------------------------------------------------------------------------------- % 3-D plots using meshgrid() %------------------------------------------------------------------------------- % What does magic square look like when plotted as a surface? % We use the meshgrid() function to generate (x,y) points on a regular grid. % Instead of creating ordered pairs, meshgrid() puts all the x values in one % array and all the y values in another array. [xx,yy] = meshgrid([1:15],[1:15]); % Now we create the z values. Normally z is a function of xx and yy. % Here, however, we create a magic square that is 15 x 15 in size, so % our xx and yy will correspond to row and column indices. z = magic(15); % Now we make our 3-D plot as a lit surface. surfl(xx,yy,z) %------------------------------------------------------------------------------- % 3-D Plot of a sinusoid %------------------------------------------------------------------------------- % Create a mesh grid on a square [-1,1] x [-1,1] with 15 pts in each direction. x1range = [-1,1]; x2range = [-1,1]; npts = 15; x1pts = x1range(1):(x1range(2)-x1range(1))/(npts-1):x1range(2); x2pts = x2range(1):(x2range(2)-x2range(1))/(npts-1):x2range(2); [xx,yy] = meshgrid(x1pts,x2pts); % Compute a cos of x+y. zz = cos(2*pi*2*(xx+yy)); % Make a surface plot. surfl(xx,yy,z) %------------------------------------------------------------------------------- % Array Processing and 3-D Plotting %------------------------------------------------------------------------------- bumps.m This script file creates bumps and sums them to create a surface. % Coming up: % Matrix inverses, pseudoinverses % Programming: control flow, functions