Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

fill in the ..... with code please Matlab Component: You are provided with a fragment of a MatLab code which calculates and plots a Fourier

fill in the ..... with code please

Matlab Component: You are provided with a fragment of a MatLab code which calculates and plots a Fourier series ( ) cos( )

1 0 y t B B t n n n

or ( ) sin( )

1 0 y t B B t n n n

. The code plots a ramp, triangle

and square periodic function with period T . The code requires inputs such as the

number of terms N to sum in the series and the number of periods to plot. In addition, the

code can sum an even or odd series as specified. The main code is given below: %Fourier_series_code clear all close all clc %Inputs num_t_points=300 %number of plotting points in t (must be even for fft operation) T=2 %Period of function (seconds) even_function=0 %1=>even, 0=>odd n_terms=200 %number of terms in Fourier series to sum n_periods_plot=3% number of periods to plot (will start at -T*((n-1)+.5)

%and end at -T*((n-1)+.5)), i.e. centered about zero function_type=3 %1=>ramp, 2=>triangle (odd), square=>3, unit amplitude spike=>4 %5=> User defined %create t_vector (plot centered around t=0) max_t=T*(n_periods_plot)/2; min_t=-T*(n_periods_plot)/2; t_vector=linspace(max_t, min_t, num_t_points); %Calculate terms in Fourier series [B_n_vector, B_0, w_n_vector]=calc_B_n_vector(n_terms, T, function_type); %Calculate f_n=w_n/(2*pi) for plotting purposes f_n_vector=w_n_vector/(2*pi); %Allocate y-function y_function=zeros(1, num_t_points); for j=1:num_t_points y_function(j)=B_0; for k=1:n_terms if(even_function==1) y_function(j)=... y_function(j)+B_n_vector(k)*cos(w_n_vector(k)*t_vector(j)); elseif(even_function==0) ................ end end end %plot y_function vs time and actual spectrum plot (B_n vs. f) figure(1) if(even_function==1) even_odd_string='even '; else even_odd_string='odd '; end %plot series approx of function subplot(2,1, 1) ............ title_string=............. title(title_string) grid on %plot series coefficients B_n vs. w_n (actual spectrum plot) subplot(2,1, 2) plot(w_n_vector, B_n_vector, 'ok') xlabel('w_n') ylabel('B_n') title_string=... ['Figure 1b: B_n vs. w_n']; title(title_string) grid on axis([0 10 -2 2]) %Adjust axes as needed for actual spectrum plot %Calculate FFT (see MatLab help fft) %Determine FFT amplitude (frequency will follow) Y = fft(y_function); %Produces two-sided (symmetric spectrum from f=-f_ny=>f_ny P2 = abs(Y/num_t_points); %Going to plot abs(B_n) not B_n P1 = P2(1:(num_t_points/2+1)); %Only rhs of distribution so half of spectrum data P1(2:end-1) = 2*P1(2:end-1); %Single-sided of symmetric spectrum is

%2*double sided B-values (neg. f) coefficients add to harmonic at f Fs=num_t_points/(T*n_periods_plot);%Calculate sample frequency (pts or samples/sec) f = Fs*[0:(num_t_points/2)]/num_t_points; %Output of fft assume max %resolution is f_nyquist or Fs/2. Create f-array of values 0=>f_nyquist=Fs/2 figure(2) plot(f,P1) title('Figure 2: Single-Sided Amplitude Spectrum of y(t)') xlabel('f (Hz)') ylabel('|B_n|') grid on axis([0 10 0 2]) %Adjust axes as needed for fft plot %Calculate Nyquist frequency and compare with highest component freq. ..................

Note that the function calc_B_n_vector is called but not provided. This function uses the function period, function type and number of terms to sum, as inputs. The outputs are the coefficient terms B0 and Bn for the series, as well as the corresponding angular velocities n . In addition, you must compare the component max frequency with the Nyquist frequency to ensure an accurate spectrum plot using FFT. Deliverables: Write an operable function calc_B_n_vector and run your program to produce: a) Ramp (upward sloping saw-tooth) b) Triangle c) Square In addition, your code must print (display) the Nyquist and max frequency component! Finally, note that some parts of the main program are missing and must be re-written! In your code, all three series will sum the first 200 terms, have a fundamental period of two seconds, plot four full periods of the cycle and populate those four periods with a total of 8000 data-points (samples).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions