0% found this document useful (0 votes)
8 views

PHP Assignment

Uploaded by

Sohaib Romie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PHP Assignment

Uploaded by

Sohaib Romie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: M Sohaib Dar

ID: 191400034

Sawtooth:
% time for sawtooth wave is gonna be T = 2*4
T = 2 * 4;
% sampling frequency is gonna be very high 1000 exactly
Fs = 1000;
%dt is going to generate discrete time whose value is take at discrete
%measurements, with the discrete time periods there will be time periods of
%n where you will have no value it means that sawtooth wave is not gonna be
%continuous
dt = 1/Fs;
%timeperiod of the sawtooth wve is goona be between -5 and 8-dt as I wrote
%8 as T was (2*4)
t=-5:dt:T-dt;
%this function will generate a sawtooth wave, the first first argument is
%gonna be the time period for which sawtooth wave will be generate and the
%second argument is the maximum location of amplitude/power that has been
%set to 0.5 and now its gonna generate a triangle wave
x = -sawtooth (pi/2*t, 0.5);
figure(1)
%this function will plot teh sawtooth wave with the time period t
plot(t,x)
% grid will be made on axis -5 x axis to 5 x axis and -1.5 y axis to y 1.5
% axis
grid on
axis ([-5 5 -1.5 1.5]);
xlabel('Time');
ylabel('Amplitude');
%sampling frequency is halved here
Fn = Fs/2;
% length is gonna return the length of the time period t
N = length(t);
FTx = fft(x)/N; % Fourier Transform
%linespace is gonna generate the amplitude spectrum
Fv = linspace(0, 1, fix(N/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector, it
specifies an element within a vector in this case let say Fv we used the
length of only Fv
figure(2)
% this is gonna plot the FFT of sawtooth power spectrum but we are only
concerned
% about the positive values of FFt because FFt is symetric
plot(Fv, abs(FTx(Iv))*2)
grid
xlabel('Frequency')
ylabel('Amplitude')
title('Fourier Transform of Sawtooth Wave')
axis([0 15 ylim])
Sine Wave:
% Sampling frequency or sampling rate refers to number of samples
% per second in a signal
Fs = 80;
% to generate a signal, we have to generate a time vector at appropriate
% sampling rate time vector of 1 second is generated
t = 0:1/Fs:1;
% sine wave of 5 Hz wi` ll be generated.
f = 4;
% sin(x) will return the sine of the elements of X
% the time period for the sine wave is gonna be 2pit having frequency f
x = sin(2*pi*t*f);
% Length of FFT (you can also consider N points of FFT as columns on signal
graph)
nfft = 1024;
% Take fft, padding with zeros so that length(X)
% is equal to nfft, in simple words next line of code will increase the
% resolution of the frequency domain, why we did padding is that we want to
see
% the frequency on a narrow brand, it also makes the DFT computation faster
X = fft(x,nfft);
% FFT is symmetric, throw away second half (it means the FFT of sign wave
% will have the same values of frequency on on negative x-axis as it has on
% positive x-axis)
X = X(1:nfft/2);
% Take the magnitude of fft of x, as in frequency graph we are only
% consider about the positive values of y
mx = abs(X);
% Frequency vector, as we are only going to take the positive half of the
% FFT so we are gonna take values of N from 0 to 511(1024/2-1) multiply it
% with the sampling frequency which is divided by NFFT=1024
f = (0:nfft/2-1)*Fs/nfft;
% Generate the plot, title and labels.
figure(1);
% make the plot of the sign wave using the time vector,t=timeVector,x sign
% wave computed above
plot(t,x);
title('Sine Wave Signal in Time Domain');
% as we have time representation on x-axis
xlabel('Time (s)');
% and amplitude representation on y-axis
ylabel('Amplitude');
figure(2);
% take frequency calculated using FFT and the positive values of frequency
% graph
plot(f,mx);
title('FFT a Sine Wave in Frequency Domain');
xlabel('Frequency (Hz)');
ylabel( 'Power');
Square Wave:
% n is the time period from 0 to 29 seceonds and 0.1 represents the
% accuracy with floating numbers
n=0:0.01:29;

% in the next line the square function is gonna generate a square with
% time period 2pi for the elements of the time array n. It is similiar to
% the sine function but the different is that it is going to generate a wave
the values on -1 and 1
x=square(2*pi*n);
% fft(x) will generate the fft of square wave it means that the the signal
% will be converted from time domain into frequency domain and the abs()
% function is just only gonna take the positive values of fft
x1=abs(fft(x));
%plot(n,x) is gonna plot the square wave wave in the time period n
%divides the current figure into rectangular panes that are numbered row-wise
%subplot(211) produces the subaxes in a figure which represents the top plot
in
%a 2 row by 1 column
subplot(211) ,plot(n,x); title ( ' Square Wave ' );
xlabel('Time');
ylabel('Amplitude');

%same is the case here but now plot() function will plat the fft of the
%square wave that was generate by fft funciton and was assigned to x1
subplot(212) ,plot(x1, 'r-x') ,title('Fourier Transform');
xlabel('Frequency');
ylabel('Power');

You might also like