class: center, middle # Introduction to DSP Eslam Adel email: `eslam.a.mahmoud@eng1.cu.edu.eg` --- class: top, left ## Signal * Varying quantity that contains information. * 1D Varying over time ![](../../images/w5/signal1.png) --- ## Signal Cont, * 2D signal Varying over space like image ![](../../images/w5/brain.jpg) --- ## Digital Signal * For limited memory * Sampling the signal $$ F_s = \frac{1}{T_s} $$ Where Fs is sampling frequency and Ts is sampling period. --- ### Sampling * For 1D signal ![](../../images/w4/sampling1.png) --- ## Sampling Cont, * For 2D signal ![](../../images/w5/sampling.PNG) --- ## Processing * Applying some operations on the signal -- * Signal enhancing, noise reduction -- * Extracting information --- ## Fourier Transform * Complex representation of the signal * Fourier Transform signal from time to frequency domain * Signal decomposition to sum of different sinusoids ![](../../images/w5/dft1.png) --- ![](../../images/w5/dft2.jpg) --- ## Signal filtering * Main step in signal processing * Used for * Enhancement * Noise cancelling * bandwidth selection ![](../../images/w5/filter1.png) ![](../../images/w5/filter.jpeg) --- ## Matlab * Variables ```matlab x = 10; y = 20; ``` * Arrays and matrices ```matlab % Define row vector myRow = [1 5 6 8 6]; % Define column vector myCol = [5; 9; 54; 84; 6] % Define a matrix myMatrix = [1 3 5; 4 8 9; 3 6 8] ``` --- ## Matlab Cont, * Colon operator ```matlab % Step is 1 a = 1:10 % Step is 0.1 b = 1:0.1:10 ``` * Element wise operation ```matlab signal = [1, 2, 5, 6]; w = [1, 0, 1, 0]; out = w .* signal; ``` --- ## Plotting ```matlab % Define x with values from -1 to 1 x = -1:0.01:1; % Define function y = f(x) and f(x) = x^2 y = x.^2; % Now plot x and f(x) in a separate figure figure, plot(x, y) % Make a title to the figure title('Plotting y = x^2') ``` ![](../../images/w5/plot.png) --- ## Control Structures * if and if else ```matlab if condition % Do something else % Do another thing end ``` * For loop ```matlab for i = 1:5 disp(i) end ``` * While loop ```matlab while(condition) % do something end ``` * switch, case --- ## Fourier Transform * Plotting signal ```matlab % Sampling Frequency Fs = 100; % Sampling Time Ts = 1/Fs; % Time interval of signal first 10 seconds t = 0:Ts:10; % Sinusoidal signal sig = sin(10.*t); % Plot the signal figure, plot(t, sig); title('Sinusoidal signal sin(10t)'); xlabel('Time in second'); ylabel('Amplitude'); ``` --- ![](../../images/w5/sin.png) --- ## Getting DFT ```matlab % To Get the Fourier domain of the signal sigFourier = fftshift(fft(sig)); % Magnitude sigFourierMag = abs(sigFourier); % Phase sigFourierPhase = angle(sigFourier); % Plotting Magnitude figure, plot(20*log10(sigFourierMag+1)); title('Magnitude of Fourier Domain of Signal sin(10t)'); ``` ![](../../images/w5/mag.png) --- ## Phase ```matlab % Plotting Phase figure, plot(sigFourierPhase); title('Phase of Fourier Domain of Signal sin(10t)'); ``` ![](../../images/w5/phase.png ) --- ## Signal Filtering Loading and plotting ECG signal ```matlab % Load Signals load('signals.mat'); % Sampling Frequency is 250 Hz Fs = 250; Ts = 1/Fs; % Lets take only 5 seconds from the signal t = 0:Ts:5; ecgSignal = sig_o4(1:length(t)); % Plot the original signal figure, plot(t, ecgSignal); title('Original Signal'); xlabel('Time in seconds'); ylabel('Amplitude') ``` --- ![](../../images/w5/origecg.png) --- Filtering the signal using average Low Pass filter ```matlab % Filtering the signal using average filter l = 9; mask = ones(1,l)/l; % Apply the filter clearEcg = conv(ecgSignal, mask,'same'); figure, plot(t, clearEcg); ``` ![](../../images/w5/filteredecg.png) --- Comparing Fourier domain of both signals ```matlab % Get FFT of original signal ecgSigFFt = fftshift(fft(ecgSignal)); % Get FFT of filtered Signal clearEcgFFt = fftshift(fft(clearEcg)); % Plot Both in same figure figure, subplot(2,1,1) plot(20*log10(abs(ecgSigFFt)+1)); title('Fourier Magnitude of Original Signal'); subplot(2,1,2) plot(20*log10(abs(clearEcgFFt)+1)); title('Fourier Magnitude of Filtered Signal'); ``` --- --- ![](../../images/w5/comparefft.png)