Thursday, September 5, 2013

VERIFICATION OF INTERPOLATION AND DECIMATION USING MATLAB

PROGRAM TO VERIFY DECIMATION AND INTERPOLATION OF A GIVEN SEQUENCE

Introduction

% Software Used: Matlab Version 7.6.0.324
% Submitted by : GG

Initial Command (Closing and Clearing)

clc;                % clears the command window
close all;          % closes all the previously open window
clear all;          % clears previously stored values

Generating Input Sequence

fm = 10;            %  input signal frequency
Fs = 140;           %  sampling frequency
t = 0:1/Fs:0.5;     % time range for the input sequence
x = sin(2*pi*fm*t); % input sinusoidal signal
figure(1)
subplot(4,1,1)
stem(x);                    % Discrete plot of the input sequence,...
                            ... where x-axis corresponds to the number of samples
xlabel('No. of samples');   % labelling x-axis
ylabel('Amplitude');        % labelling y-axis
title('input discrete sinusoidal sequence'); % giving title to the plot





% Inference: A sinsoidal input signal was generated with the signal
% frequenccy of 10 Hz and was again sampled using the sampling frequency of
% 140 Hz which is well above to satisfy the nyquist criterion.So, we
% obtained 72 samples from 0 to 0.5 sec when sampled at 140 Hz. The
% obtained sinusoidal sequence was subjected to discrete plot using
% stem plot option which plots the sampled signal amplitude with respect
% to the number of samples. The first plot of the figure corresponds to
% the generated sinusoidal sequence


Decimation of the Input Sequence

M = 2;              % factor by which the input sequence is decimated
xd = decimate(x,M); % resamples the sample in x with a rate (1/M) times the original rate
subplot(4,1,2)
stem(xd)            % Discrete plot of the input sequence,...
                    ... where x-axis corresponds to the number of samples
xlabel('No. of samples'); % labelling x-axis
ylabel('Amplitude');      % labelling y-axis
title('Decimated Sinusoidal Sequence'); % giving title to the plot


% Inference: The obtained sinusoidal sequence was subjected for the
% decimation by the factor M = 2. The process of decimation involves
% resampling the sequence in low data rate after the process of low pass
% filtering.Since the given sequence is decimated by the factor 2,
% the resultant sequence after the decimation should have exactly
% half the number of samples than that of the original sequence.
% It is clearly seen in the plot that the decimated sequence has
% 36 samples in comarison to 72 of the original sample. Hence
% the decimation of a sequence is verified.


Interolation of the Input Sequence

L = 2;              % factor by which the input sequence is interpolated
xI = interp(x,L);   % resamples the sample in x with a rate L times the original rate
subplot(4,1,3);
stem(xI);           % Discrete plot of the input sequence,...
                    ... where x-axis corresponds to the number of samples
xlabel('No. of  samples');  % laeblling x-axis
ylabel('Amplitude');        % labelling y-axis
title('Interpolated Sinuoidal Sequence')   % giving title to the plot




% Inference: The obtained sinusoidal sequence was also subjected for the
% interpolation by the factor L = 2. The Process of interpolation involves
% resampling the sequence at higher sampling rate using low pass filter.
% Since the interpolation factor is 2, the samples in interpolated signal
% must double than that of the original signal and it can be verfied from
% the plots obtained. Hence the interpolation is also verfied.


Interpolation of the Decimated Signal

L = 2;              % coefficient by which the singal is interpolated
xI = interp(xd,L);  % resamples the sample in x with a rate L times the original rate
subplot(4,1,4)
stem(xI);           % Discrete plot of the input sequence,...
                    ... where x-axis corresponds to the number of samples
xlabel('No. of  samples'); % labelling x-axis
ylabel('Amplitude');       % labelling y-axis
title('Original Signal Obtained After Interpolating the Decimated Signal'); % giving title to the graph

% Inference: When a sequence is decimated by a certain factor and then
% again subjected to the interpolation by the same factor, original
% sequence can be recovered. This has been verified by the 4th plot
% on the figure, which is the interpolation of the decimated signal in the
% 2nd plot. The 4th plot obtained is similar to the input sequence. So,
% interpolating a decimated signal or decimating a interpolated signal
% gives the original signal.





Wednesday, September 4, 2013

TITLE:  Generation of Different Basic Signals Using Matlab 

Software Used: Matlab Version 7.6.0

Initial Command

clc; %clear command window
close all;% close all opened window
clear all; %clear all the previously stored values

Generation of Impulse Sequence

continuous time impulse sequence

N = 50; % the variablel N is given a value to be used for the size of t and n
t = -N:1:N; % range of t extends from -N to N
x = zeros(1,length(t)); % an array x is taken such that it has a row of zeroes eqal to the length of t
x(1,51) = 1; % 51st element of the array is taken as 1, that lies at t =0.
figure(1)
subplot(2,1,1) %
plot(t,x,'r') % the continuous unit step funtion plotted with red colored line
xlabel('Time'); %labelling of x-axis
xlabel('Amplitude'); % labelling of y-axis
title('continuous time unit impulse sequence'); % giving title to the graph

% discrete time impulse sequence
n = -N:1:N; % range of N extends from -N to N with a gap of 1 between them
xn = zeros(1,length(n)); % an array is taken such that it has a row of zeroes equal to the lenght of sample 'n'
xn(1,51) = 1; % 51st sample is taken as 1 which is at n=0
subplot(2,1,2)
stem(n,xn); % discrete stem plot of the impulse sequence
xlabel('discrete samples'); %labelling x-axis
ylabel('Amplitude'); % labelling y-axis
title('discrete time unit impulse sequence'); % title to the graph



Inference:

Unit Impulse function is a function which is zero at all other places except at 0. The function was realized using the matlab for both the continuous and discrete time domain plot. The graph in both the cases had same properties except for the fact that one was discrete and the other continuous.

Generation of Unit Step Sequence

continuous time unit step sequence

x = ones(1,length(t)); % an array x is taken such that it has a row of ones eqal to the length of t
x(1,1:51) = 0; % 51st sample is taken as 0 which is at t=0
figure(2)
subplot(2,1,1)
plot(t,x, 'r'); %plotting of the continuous unit step sequence with red color
xlabel('Time'); % labelling of x-axis
xlabel('Amplitude'); %labelling y-axis
title('continuous time unit step sequence'); % title to the graph

% continuous time unit step sequence
xn = ones(1,length(t)); % an array is taken such that it has a row of 0nes equal to the lenght of sample 'n'
xn(1,1:51) = 0; % 51st sample is taken as 0 which is at n=0
subplot(2,1,2)
stem(n,xn); % discrete plot of the unit step sequence
xlabel('discrete samples'); %labelling x-axis
ylabel('Amplitude'); % labelling y-axis
title('discrete time unit step sequence'); % title to the graph



Inference:

Unit Step function has value 0 for all the negative arument and one for the positive argument. This function was implemented successfully using the matlab code for both the continuous time and discrete time domain.

Generation of ramp function

continuous time ramp function

t = 0:0.1:N; % time interval for the ramp function choosen from 0 to N with a interval of 0.1
m = 1; %slope of the ramp function
x = m*t; % equation depicting ramp function
figure(3)
subplot(2,1,1)
plot(t,x); % continuous plotting of ramp function
xlabel('time'); %labelling x-axis
ylabel('Amplitude'); % labelling y-axis
title('continuous time ramp function') % title to the graph

% discrete time ramp function
n =0:1:N; % samples for the discrete time ramp function from 0 to N.
xn = m*n; % equation depicting discrete time ramp function
subplot(2,1,2)
stem(n,xn); % discrete plot
xlabel('discrete samples'); % labelling x-axis
ylabel('Amplitude'); % labelling y-axis
title('Discrete time ramp function') % title of the graph



Inference:

Ramp funtion can be represented by a simple linear equation passing through the origin. Its value is always zero in negative x-axis. The equation was successfully represented and plotted in the matlab in both
 continuous time and discrete time domain.

Generation of Sinusoidal Function

continuous time sinusoidal function

t = 0:0.01:1; % time range for the sinusoidal function
x = sin(2*pi*5*t); % equation depicting sinusoidal signal
figure(4)
subplot(2,1,1)
plot(t,x); % continuous plot of signal x with respect to t.
xlabel('time');% labelling x-axis
ylabel('Amplitude'); %labelling y-axis
title('continuous time sinusoidal function') % title of the graph

% discrete time sinusoidal function
n = 0:0.01:1; % number of samples for the sinusoidal wave
figure(4)
xn = sin(2*pi*5*n); % equation depicting discrete sinusoidal equation
subplot(2,1,2);
stem(n,xn); % discrete plot of the sine wave
xlabel('discrete samples'); % labelling x-axis
ylabel('amplitude'); % labelling y-axis
title('discrete time sinusoidal function'); %title to the graph



Inference:

Sinusoidal  sine waves were also successfully coded in continuous and discrete time domain using matlab and the graphs were obtained. The behaviour of the graph was as expected

Generation of exponential signal

continuous time exponential function

a = 2; %coeffficient of exponential function
t = 0:0.01:2; % time range for exponential funtion
x =exp(a*t); % equation depicting exponential function
figure(5)
subplot(2,1,1)
plot(t,x) % continuous plot of exponential funtion
xlabel('time'); % labelling x-axis
ylabel('amplitude') % labelling y-axis
title('continuous exponential function'); % title to the graph

% discrete time exponential function
a = 2; %coeffficient of exponential function
x =exp(a*t);% equation depicting exponential function
figure(5)
subplot(2,1,2)
stem(t,x) % discrete plot of the exponential function
xlabel('time'); % labelling x-axis
ylabel('amplitude') % labelling y-axis
title('discrete time exponential function'); % title to the graph



Inference:

Exponential signals were also represented in discrete and continuous time domain using matlab code. The behavior of the graphs obtained was as expected.The above graph was obtained for positive exponential functiom and the same code can generate the negative exponential function by simply changing the coefficient of exponential funtion to some negative value.

Generation of Triangular wave

continuous triangular wave generation

y = 0:0.1:1; % the amplitude of the triangular wave limited between 0 and 1.
figure(6)
subplot(2,1,1)
for i = 0:3  % for loop implemented for the first half of the ramp signal in triangular wave
    x = y +(2*i); % generalized equation found using two point formula for the positive ramp of the triangular wave
    plot(x,y) % plotting the graph for the above generalized equation
  hold on % the plotted value of x and y is kept on hold
end % end of for loop
for i = 1:3 % for loop implemented for the second half of the ramp signal in triangular wave

    x = (2*i)-y;  % generalized equation found using two point formula for the negative ramp of the triangular wave
    plot(x,y) %plotting the graph for the above generalized equation
    hold on; % the plotted value of x and y is kept on hold
end % end of for loop
xlabel('time'); % labelling x-axis
ylabel('amplitude'); % labelling y-axis
title('Continuous triangular wave signal') % title of the graph
hold off; % the hold is removed.

% discrete triangular wave generation
y = 0:0.1:1; % the amplitude of the triangular wave limited between 0 and 1.
figure(6)
subplot(2,1,2)
for i = 0:3  % for loop implemented for the first half of the ramp signal in triangular wave
    x = y +(2*i); % generalized equation found using two point formula for the positive ramp of the triangular wave
   stem(x,y) % discrete plotting the graph for the above generalized equation
  hold on % the plotted value of x and y is kept on hold
end % end of for loop
for i = 1:3 % for loop implemented for the second half of the ramp signal in triangular wave

    x = (2*i)-y;  % generalized equation found using two point formula for the negative ramp of the triangular wave
    stem(x,y) %discrete plotting the graph for the above generalized equation
    hold on; % the plotted value of x and y is kept on hold
end % end of for loop
xlabel('time'); % labelling x-axis
ylabel('amplitude'); % labelling y-axis
title('Discrete triangular wave signal') % title of the graph
hold off; % the hold is removed.



Inference:

Triangular wave was also obtained using the matlab code. To obtain the graph for triangular wave, for loop was extensively used. The behaviour of the graph was as expected. Discrete plot was also obtained using the stem function.

Generation of sawtooth wave

sawtooth wave generation

figure(7)
subplot(2,1,1)
y = 0:1; % the amplitude of the sawtooth wave limited between 0 and 1
for i = 0:5 %  for loop implemented for the first half of the ramp signal in sawtooth wave
    x = y -i; % generalized equation for the positive ramp of the sawtooth wave.
    plot(x,y) % plotting of the above generalized equation
    hold on; % the values of x and y and the plot is kept on hold
end % end of for loop
x = -4:1:1; % value of x-axis limited between -4 and 1
for j = 0:0.001:1; % for loop implemented for the other half of the sawtooth wave
    y = j; % all the value of j between 0 and 1 is assigned as y with a small interval of 0.001
    plot(x,y); % plotting of the graph of x and y to obtain a straight line connecting the ramp to the x-axis
    hold on; % the plotted values ar kept on hold
end % end of for loop
xlabel('time'); %labelling x-axis
ylabel('amplitude'); % labelling y-axis
title('Continuous sawtooth wave signal'); % title of the graph
hold off; % the hold is removed

figure(7)
subplot(2,1,2)
y = 0:0.1:1; % the amplitude of the sawtooth wave limited between 0 and 1
for i = 0:5 %  for loop implemented for the first half of the ramp signal in sawtooth wave
    x = y -i; % generalized equation for the positive ramp of the sawtooth wave.
    stem(x,y) % discrete plotting of the above generalized equation
    hold on; % the values of x and y and the plot is kept on hold
end % end of for loop
x = -4:1:1; % value of x-axis limited between -4 and 1
for j = ones(length(x)); % for loop implemented for the other half of the sawtooth wave
    y = j; % all the value of j between 0 and 1 is assigned as y with a small interval of 0.001
    stem(x,y); % discrete plotting of the graph of x and y to obtain a straight line connecting the ramp to the x-axis
    hold on; % the plotted values ar kept on hold
end % end of for loop
xlabel('time'); %labelling x-axis
ylabel('amplitude'); % labelling y-axis
title(' Discrete sawtooth wave signal'); % title of the graph
hold off; % the hold is removed



Inference:

Sawttooth wave obtained using the matlab coding was as expected. Limiting of the value of amplitude to calculate the time period was used here as in the triangualr wave above.

Generation of square wave

continuous square wave

m = 0:0.001:1; % variable m assigned value from 0 to 1 with a intrval of 0.001
figure(8)
subplot(2,1,1)
for i = 0:1:10; % for loop implemented to assign the value of the square wave funtion at x = 0 to 10 with a interval of 1
    x = i;
    y = m; % amplitude assigned with a value increasing from 0 to 1.
    plot(x,y); % continous plot of x and y
    hold on; % the plotted value is kept on hold
end % end of foor loop

for j = 0:2:10;  % another for loop implemented to assign the maximum value to the square wave at fixed time intervals
    x = j + m; % the square wave function from 0 to 1, 2 to 3, 4 to 5 and so on taken into consideration
    y = 1; % functional value at those points assigned as one
    plot(x,y) % obtained value of y and x is plotted
    hold on; % value kept on hold for further use
end % end of for loop
for k = 1:2:10; %for loop implemented to assign the minimum value to the sqare wave at fixed time intervals
    x = k + m; %the square wave function from 1 to 2, 3 to 4, 5 to 6 and so on taken into consideration
    a = 0; % functional value at those point assigned as zero
    plot(x,a) %plot of x and y is done
    hold on; % obtained plot is kept on hold
end % end of for loop
hold off; % hold is called off
xlabel('time'); % labelling of x-axis
ylabel('amplitude'); % labelling of y-axis
title('Continuous square wave signal') % title to the graph
axis ([-5 15 -0.5 1.5]); % axis of the plot is fixed

% Discrete Square Wave
m = 0:0.2:1; % variable m assigned value from 0 to 1 with a intrval of 0.001
figure(8)
subplot(2,1,2)
for j = 0:2:10;  % another for loop implemented to assign the maximum value to the square wave at fixed time intervals
    x = j + m; % the square wave function from 0 to 1, 2 to 3, 4 to 5 and so on taken into consideration
    y = ones(length(x)); % functional value at those points assigned as one
    stem(x,y) % obtained value of y and x is discrete plotted
    hold on; % value kept on hold for further use
end % end of for loop
for k = 1:2:10; %for loop implemented to assign the minimum value to the sqare wave at fixed time intervals
    x = k + m; %the square wave function from 1 to 2, 3 to 4, 5 to 6 and so on taken into consideration
    a = zeros(length(x)); % functional value at those point assigned as zero
    stem(x,a) % discrete plot of x and y is done
    hold on; % obtained plot is kept on hold
end % end of for loop
hold off; % hold is called off
xlabel('time'); % labelling of x-axis
ylabel('amplitude'); % labelling of y-axis
title('Discrete square wave signal') % title to the graph
axis ([-5 15 -0.5 1.5]); % axis of the plot is fixed



Inference:

square waves are periodic in nature whose amplitude varies between two fixed values and the time period in each value is equal. Sqaure wave was obtained just as explained by the defination using the matlab code. For loop was used to achieve the wave.Similarly Square wave was also obtained in discrete time, using the stem function.



Matlab Code to Enchipher and Dichipher Using Caesar Cipher

Caesar Cipher


The enciphering and Deciphering using Ceaser Cipher  has been illustrated below using the matlab code. Ansii decimal number for each letter is taken into consideration for the process. The code here is only suitable for the lower case letters without any spaces in between and it can be subsequently modified for Upper case letters using the corresponding ansii code. 

Initially when the code is run, it asks  for the key for the cipher. The Key must be in between 1 and 26. Once the key is fixed, the text to be ciphered has to be entered within single inverted comma in lower case letter without any spaces in between. Now, the given input text is ciphered.

Now, this code also does deciphering. Since the ciphered text has only 26 available combinations, all those combinations are tried and given as output of the deciphered text.

%% Matlab code to encipher and Decipher Caesar Cipher

% By: GG
% This is the demo code which enciphers and Deciphers using caesar cipher
% for all the lower case letters

%% inital command (clearing and closing)

clc
clear all
close all

%% input parameter

xin = input('Enter the cipher key xin_key_26; xin(between 1 and 26):  ' );  % enter the mode of ceaser cipher i.e. xmod26, here x = xin defines the shift
text_to_be_ciphered =input('ENter the text to be ciphered: ');

%% output_ciphered_parameter

ciphered_text = char(rem(text_to_be_ciphered + xin - 97,26)+97) % using the ansii decimal number for small letters which is from 97 to 122 ,...
...  were 97 corresponds a and 122 corresponds z, subtracting 97 results from 0 to 25;  
... and adding xin will provide the shift. remainder after division by 26 will give decimal number from 0 to 25  adding 97 to it corresponds to a to z. 

%% diciphered_text_crptoanyalysis

for pin = 1:26;
diciphered_text = char(rem(ciphered_text-pin - 97 +26,26)+97) % decipher is done in the similar way as encipher only difference is xin is subtracted rather than adding.
end