clear all; close all; clc; %% Question 5 t = -2:0.01:2; y = rect(t); plot(t, y); xlim([-3 3]); ylim([-2 2]); xlabel('t'); ylabel('rect(t)'); title('rect(t) vs t'); %% Question 6 T0 = 2; f0 = 1/T0; Ts = T0/10; t = -10*T0:Ts:10*T0; s = sinc(f0*t); figure(2); plot(t, s); xlabel('t'); ylabel('s(t)'); title('Signal s(t) vs t'); grid on; % We observe that A sinc pulse passes through zero % at all positive and negative integers (i.e., t = ±1, ±2, ±3, ...), % but at t=0, it reaches its maximum of 1. %% Question 7 t = -2:0.01:2; f1 = 10; x1 = 2*sin(2*pi*f1*t); figure(3); plot(t, x1); xlabel('t'); ylabel('x1(t)'); title('Signal x1(t) vs t'); x2 = 2 + x1; figure(4); plot(t, x2); xlabel('t'); ylabel('x2(t)'); title('Signal x2(t) vs t'); f3 = 10*f1; x3 = sin(2*pi*f3*t); figure(5); plot(t, x3); xlabel('t'); ylabel('x3(t)'); title('Signal x3(t) vs t'); x4 = x1.*x3; figure(6); plot(t, x4); xlabel('t'); ylabel('x4(t)'); title('Signal x4(t) vs t'); %% Question 8 % 8.a Ts = 1/50; t = 0:Ts:10-Ts; x = sin(2*pi*15*t) + sin(2*pi*20*t); % 8.b X = fft(x); fs = 1/Ts; f = (0:length(X)-1)*fs/length(X); % 8.c figure(7); subplot(2,1,1); plot(t,x); xlabel('Time (seconds)') ylabel('Amplitude') subplot(2,1,2); plot(f,abs(X)); xlabel('Frequency (Hz)') ylabel('Magnitude') title('Magnitude') % 8.d % When we plot the magnitude of the signal as a % function of frequency, the spikes in magnitude % correspond to the signal's frequency components % of 15 Hz and 20 Hz. %% 8.e % Changing the length of the signal % keeping it 5 seconds (instead of 10 seconds) Ts = 1/50; t = 0:Ts:5-Ts; x = sin(2*pi*15*t) + sin(2*pi*20*t); X = fft(x); fs = 1/Ts; f = (0:length(X)-1)*fs/length(X); figure(8); subplot(2,1,1); plot(t,x); xlabel('Time (seconds)') ylabel('Amplitude') subplot(2,1,2); plot(f,abs(X)); xlabel('Frequency (Hz)') ylabel('Magnitude') title('Magnitude') % We find that there is no difference in the solution % except for the case that A longer FFT will divide up % the frequency range into more "bins" between 0 and Fs/2. % So the result bin of a particular frequency may be % in a different bin number for different lengths of FFT. %% 8.f % Changing the sampling period of the signal % keeping it 1/20 seconds (instead of 1/50 seconds) Ts = 1/20; t = 0:Ts:10-Ts; x = sin(2*pi*15*t) + sin(2*pi*20*t); X = fft(x); fs = 1/Ts; f = (0:length(X)-1)*fs/length(X); figure(9); subplot(2,1,1); plot(t,x); xlabel('Time (seconds)') ylabel('Amplitude') subplot(2,1,2); plot(f,abs(X)); xlabel('Frequency (Hz)') ylabel('Magnitude') title('Magnitude') % Since the FFT puts the signal into as many frequency bins % as there are signal samples, so more samples we have % in the time domain, the better our resolution in the % frequency domain will be that is more bins % therefore smaller bandwidth of each bin %% 8.g Ts = 1/500; Vth = 1; Vin = 5; t = 0:Ts:1; fs = 10; y1 = Vin*sin(2*pi*fs*t); y = Vin*sign(y1 - Vth); X = fft(y); f = (0:length(X)-1)*fs/length(X); figure(10); subplot(2,1,1); plot(t,y); xlabel('Time (seconds)') ylabel('Amplitude') subplot(2,1,2); plot(f,abs(X)); xlabel('Frequency (Hz)') ylabel('Magnitude') title('Magnitude') % we get the frequency value in the multiples of 0.2 % like 0.2, 0.4, 0.6, etc using a sampling frequency of 10 Hz and % sampling period of 1/500 %% Functions function [y] = rect(t) y = zeros([1 numel(t)]); idx = (t<=0.5) & (t>=-0.5); y(idx) = 1; end function [y] = sinc(t) y = sin(pi*t)./(pi*t); end