Samar Qureshi
1009149583
Find the FS coefficients for a sawtooth wave. Use the period To = 10^(-3). Choose a suitable number of points and experiment with different numbers of points, e.g. N=100, N=200. Now, determine the original signal from the FS coefficients and see how the accuracy of the reconstructed signal depends on the number of points which is a function of the sampling frequency. Plot the magnitude and phase of the coefficients.

Magnitude and phase plots of the sawtooth FS coefficients.
To = 1e-3; fo = 1/To;
% compute FS coefficients
function [Xc, kc, fc] = fs_coeffs(x, To)
N = numel(x);
X = (1/N)*fft(x); % FS coefficients over k = 0..N-1
Xc = fftshift(X); % center DC at middle
kc = (-floor(N/2)):(ceil(N/2)-1); % harmonic indices (centered)
fc = kc / To; % frequencies = k/T0
end
% reconstruct time samples from FS
function xr = fs_recon(X, To)
% X is ONE period, (1/N)*fft(x) style; inverse is ifft(N*X)
N = numel(X);
xr = ifft(N*X,'symmetric');
end
% 99% BW definition helper
function [K99, frac, order] = fs_99percent(Xc)
% Xc is centered spectrum (fftshifted)
Pk = abs(Xc).^2;
[Pk_sorted, order] = sort(Pk,'descend');
cumfrac = cumsum(Pk_sorted)/sum(Pk_sorted);
K99 = find(cumfrac>=0.99,1,'first');
frac = cumfrac(K99);
end
N = 100; % try 100, then 200
t = (0:N-1)*(To/N);
% sawtooth on [0,To] with range [-1,1]
x = 2*(t/To) - 1;
x = x - mean(x);
% FS coefficients
[Xc, kc, fc] = fs_coeffs(x, To);
% Plots
figure; subplot(2,1,1); stem(kc, abs(Xc), 'filled'); grid on;
xlabel('Harmonic k'); ylabel('|X_k|'); title('Sawtooth: magnitude');
subplot(2,1,2); stem(kc, angle(Xc), 'filled'); grid on;
xlabel('Harmonic k'); ylabel('\\angle X_k (rad)'); title('Sawtooth: phase');
X = ifftshift(Xc); xr = fs_recon(X, To);
fprintf('RMS recon error (N=%d): %g\\n', N, rms(x - xr));
% power capture
[K99, frac, order] = fs_99percent(Xc);
fprintf('Sawtooth: %d coefficients capture %.1f%% power\\n', K99, 100*frac);
Find the FS coefficients (plot magnitude and phase) for a half-wave rectified sine wave. Choose the origin so that the signal is even, and To = 10^(-3). Again, consider different choices for the number of points in one period.

To = 1e-3; fo = 1/To;
% compute FS coefficients
function [Xc, kc, fc] = fs_coeffs(x, To)
N = numel(x);
X = (1/N)*fft(x); % FS coefficients over k = 0..N-1
Xc = fftshift(X);
kc = (-floor(N/2)):(ceil(N/2)-1);
fc = kc / To;
end
% reconstruct time samples from FS
function xr = fs_recon(X, To)
N = numel(X);
xr = ifft(N*X,'symmetric');
end
% 99% BW definition helper
function [K99, frac, order] = fs_99percent(Xc)
Pk = abs(Xc).^2;
[Pk_sorted, order] = sort(Pk,'descend');
cumfrac = cumsum(Pk_sorted)/sum(Pk_sorted);
K99 = find(cumfrac>=0.99,1,'first');
frac = cumfrac(K99);
end
N = 200; t = (0:N-1)*(To/N); w0 = 2*pi*fo;
% [0, To/2], mirror to [To/2, To]
x_half = sin(w0*t); x_half(x_half<0) = 0; % half-wave rectified over full grid
x_even = x_half;
x_even( (N/2+1):N ) = x_even( N/2:-1:1 ); n
x = x_even;
[Xc, kc, fc] = fs_coeffs(x, To);
figure; subplot(2,1,1); stem(fc/1e3, abs(Xc), 'filled'); grid on;
xlabel('f (kHz)'); ylabel('|X(f)|'); title('Half-wave rectified sine: |X|');
subplot(2,1,2); stem(fc/1e3, angle(Xc), 'filled'); grid on;
xlabel('f (kHz)'); ylabel('\\angle X'); title('Phase');
[K99, frac] = fs_99percent(Xc);
fprintf('Half-wave: %d coefficients capture %.1f%% power\\n', K99, 100*frac);
*Define the time signal x(t) on the interval [0,To], where To = 10^(-3) as x(t) = cos(2pifo(t + (1/(2*To))t^2)) where fo=1/To. This is known as a chirp signal. Find the Fourier series coefficients using a suitable number of points in the sampled signal, i.e a suitable sampling frequency.