Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

B. Gibbs Phenomenon when the fourier Series is truncated (not all harmonies are

ID: 2248098 • Letter: B

Question

B. Gibbs Phenomenon when the fourier Series is truncated (not all harmonies are included a phenomenon called the Gibbs oscillation occurs To fundamental period in seconds of the square wave Sao DC component t Range - plotting range for signal, 2xl vector n _vec (1N); Omega0 E 2"pi/T0;for 1/70; From Wikipedia (https://en.wikipedia.org/wiki/Gibbs phenomenon) : "in mathematics, the Gibbs phenomenon, discovered by Henry Wilbraham (1848) and rediscovered by J. WElard Gibbs (U1899), is the peculiar manner in which the Fourier series of a piecewise continuously differentiable periodic function behaves at a jump discontinuity. The nth partial sum of the Fourier series has large oscillations near the jump, which might increase the maximum of the partial sum above that of the function itself. The overshoot does not die out as in increases, but approaches a finite limit. This sort of behavior was also observed by experimental physicists, but was believed to be due to imperfections in the measuring apparatuses. This is one cause of ringing artifacts in signal processing." Let {} be the Fourier Series coefficients of fe). Define & Compute the Fourier series coefficients for a square wave a_k - zeros (size (a vec ); O odd indexed components only have non-zero values a _k(1:2end ) = 1.1%n vec (12tend) Omegao ); x, ()- €ce dt = 1/N£010); t = [Range(1):dt : Range (2) 1; xt = zeros (size (t )); form - 1: length(R_vec) xt - xt +a _k (m) *expavec (m) Omega0t) +con (a k(tm))exp Jºn_vec (m) Omega0't ); end xt = xt + ad*ones (size (t ); This is the synthesis equation for Fourier Series with a finite number of sinusoids. function (xt, dt) - FSsynthesis_Square(N, To, a0, Range ); 8 N number of components to use figure(); plot (t, xt) title('Fourier approximation for Ne , num2stEN )); labelltime ) ylabel(x (t )"); 'numa EOW The code above generates the signal for a square wave with a period of 4 with a single period taking the form; 10st

Explanation / Answer

original Gibbs sampler written in C:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <gsl/gsl_rng.h>

#include <gsl/gsl_randist.h>

void main()

{

int N=50000;

int thin=1000;

int i,j;

gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);

double x=0;

double y=0;

printf("Iter x y ");

for (i=0;i<N;i++) {

for (j=0;j<thin;j++) {

x=gsl_ran_gamma(r,3.0,1.0/(y*y+4));

y=1.0/(x+1)+gsl_ran_gaussian(r,1.0/sqrt(2*x+2));

}

printf("%d %f %f ",i,x,y);

}

}

This is a straightforward application of the GSL library. To create a mex file out of this, I extracted the numeric component of the code and saved it as djwsampler.csnip:

int i,j;

gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);

double x=0;

double y=0;

for (i=0;i<N;i++) {

for (j=0;j<thin;j++) {

x=gsl_ran_gamma(r,3.0,1.0/(y*y+4));

y=1.0/(x+1)+gsl_ran_gaussian(r,1.0/sqrt(2*x+2));

}

samples[i] = x;

samples[i+N] = y;

}

The Matlab interface

This is all we need to write in terms of C code. As you can see from the code above, I’ve removed the hard-coded definitions of N and thin, and instead of writing out the samples with printf, I am storing them in a samples vector of size Nx2. Thus, in Matlab, I define the two input arguments and the output argument, and then call mexme to automagically write the mex wrapper, then compile this wrapper. Thus:

inputargs = [InputNum('N',true,true,'int32');

InputNum('thin',true,true,'int32')];

outputargs = [OutputNum('samples','N,2')];

opts.extraincludes = readfile('djwsampler.includes');

cfile = mexme('djwsampler.csnip',inputargs,outputargs,opts);

writefile('djwsamplermex.c',cfile)

mex -lgsl -lgslcblas djwsamplermex.c

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <gsl/gsl_rng.h>

#include <gsl/gsl_randist.h>

void main()

{

int N=50000;

int thin=1000;

int i,j;

gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);

double x=0;

double y=0;

printf("Iter x y ");

for (i=0;i<N;i++) {

for (j=0;j<thin;j++) {

x=gsl_ran_gamma(r,3.0,1.0/(y*y+4));

y=1.0/(x+1)+gsl_ran_gaussian(r,1.0/sqrt(2*x+2));

}

printf("%d %f %f ",i,x,y);

}

}

This is a straightforward application of the GSL library. To create a mex file out of this, I extracted the numeric component of the code and saved it as djwsampler.csnip:

int i,j;

gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);

double x=0;

double y=0;

for (i=0;i<N;i++) {

for (j=0;j<thin;j++) {

x=gsl_ran_gamma(r,3.0,1.0/(y*y+4));

y=1.0/(x+1)+gsl_ran_gaussian(r,1.0/sqrt(2*x+2));

}

samples[i] = x;

samples[i+N] = y;

}

The Matlab interface

This is all we need to write in terms of C code. As you can see from the code above, I’ve removed the hard-coded definitions of N and thin, and instead of writing out the samples with printf, I am storing them in a samples vector of size Nx2. Thus, in Matlab, I define the two input arguments and the output argument, and then call mexme to automagically write the mex wrapper, then compile this wrapper. Thus:

inputargs = [InputNum('N',true,true,'int32');

InputNum('thin',true,true,'int32')];

outputargs = [OutputNum('samples','N,2')];

opts.extraincludes = readfile('djwsampler.includes');

cfile = mexme('djwsampler.csnip',inputargs,outputargs,opts);

writefile('djwsamplermex.c',cfile)

mex -lgsl -lgslcblas djwsamplermex.c