Wednesday, 16 March 2011

Amplitude Modulation_matlab


% MATLAB Script for Amplitude Modulation
% Although it is possible to modulate any signal over a sinusoid, however I
% will use a low frequency sinusoid to modulate a high frequency sinusoid
% without the loss of generality.


format long;
% Clear all previuosly used variables and close all figures
clear all;
close all;
% Amplitude, Frequency and Phase Shift for Modulating Signal
A1 = 2; f1 = 5; p1 = 0;
% Amplitude, Frequency and Phase Shift for Carrier Signal
A2 = 4; f2 = 20; p2 = 0;
% Sample Rate - This will define the resolution
fs = 1000;
% Time Line. Longer the signal, better will be the fft
t = 0: 1/fs : 1;
% Generate the message signal
s1 = A1*sin(2*pi*f1*t + p1);
% Plot the message signal
figure(1);
plot(t,s1);
xlabel('Time (sec)');
ylabel('Amplitude');
title(['Message Signal with frequency = ',num2str(f1),' Hz']);
grid on;
% Generate the Carrier wave
s2 = A2*sin(2*pi*f2*t + p2);
% Plot the carrier wave
figure(2);
plot(t,s2);
xlabel('Time (sec)');
ylabel('Amplitude');
title(['Carrier Signal with frequency = ',num2str(f2),' Hz']);
grid on;
% Finally the Modulation
% Ref. Modern Analogue and Digital Communication Systems - B. P. Lathi
% Amplitude Modulation with Suppressed Carrier
% Double Sideband with Suppressed Carrier (DSB-SC)
s3 = s1.*s2;
% Generate the Envelope
s3_01 = A1*A2*(sin(2*pi*f1*t));
s3_02 = -A1*A2*(sin(2*pi*f1*t));
% Amplitude Modulation with Large Carrier
% Double Sideband with Large Carrier (DSB - LC)
s4 = (A2 + s1).*sin(2*pi*f2*t);
% Generate the Envelope
s4_01 = A2 + s1;
s4_02 = -A2 - s1;
% ----------------------------------------------------------
% Let's Check out the frequency content of the two Modulations
% Number of FFT points. N should be greater than Carrier Frequency
% Larger the better
N = 2^nextpow2(length(t));
f = fs * (0 : N/2) / N;
% Find FFT
s3_f = (2/N)*abs(fft(s3,N));
s4_f = (2/N)*abs(fft(s4,N));
%-------------------------------------------------------------
% Plot the two Modulations
% Plot the DSB-SC Signal
figure(3);
subplot(2,1,1);
plot(t,s3);
hold on;
plot(t,s3_01,'r');
hold on;
plot(t,s3_02,'g');
xlabel('Time (sec)');
ylabel('Amplitude');
title('Double Sideband with Suppressed Carrier');
grid on;
subplot(2,1,2);
plot(f(1:100),s3_f(1:100));
xlabel('Frequency (Hz)');
ylabel('| Amplitude |');
title('Spectral Anaalysis (Single Sided PSD)');
grid on;
% Plot the DSB-LC Signal
figure(4);
subplot(2,1,1);
plot(t,s4);
hold on;
plot(t,s4_01,'r');
hold on;
plot(t,s4_02,'g');
xlabel('Time (sec)');
ylabel('Amplitude');
title('Double Sideband with Large Carrier');
grid on;
subplot(2,1,2);
plot(f(1:100),s4_f(1:100));
xlabel('Frequency (Hz)');
ylabel('| Amplitude |');
title('Spectral Anaalysis (Single Sided PSD)');
grid on;

Edge detection


Feature detection is an importatnt aspect of any image or video processing application. The given code can be used for detecting cornera and edges in a RGB or grayscale image.It is based upon the famous paper on the topic titled
 "A Combined Corner and Edge Detector" by Harris and stephens.





%%%% This function can be used for corner and edge detection
%%%% for any RGB or grayscale image. It is based upon the famous
%%%% paper on the topic titled "A Combined Corner and Edge Detector"
%%%% by Harris and stephens.
 
%%%%%%%%%%%%% input arguments
%%%%% input = The input image whose corners and edges are to be detected.
%%%%% sigma(optional) = Standard deviation of gaussian filter . Default is
%%%%% 1.
%%%%% kernelsize(optional) = The size of gaussian kernel. Default is 8.
%%%%% thresh(optional) = threshhlod value to be used as described in the
%%%%% algorithm. Default is 0.002.
%%%%% ratio(optional) = The algorithm requires that gradients be convolved
%%%%% with a larger gaussian the second time around. This is the ratio of the
%%%%% of sigma to be used second time to that used first time. Default is
%%%%% 1.5.
 
%%%%%%%%%%%% output arguments
%%%%%% O= output binary image containing detected corners and regions.
%%%%%%% Example:-
%%%%%%%%%%%% O = feature_detection('bus10.jpg',1(sigma),8 (kernelsize),
%%%%%%%%%%%% 0.002 (thresh),1.5(ratio));
 
 
function O=feature_detection(input,varargin)
%%%% validate the arguments
error(nargchk(1,5,nargin));
%%% assign values to the input paraameters as per number of arguments
%%% specified by the user.
sigma=1;
kernelsize=8;
thresh=0.002;
ratio=1.5;
if(nargin>1)
sigma=varargin{1}(:);
end
if(nargin>2)
kernelsize=varargin{2}(:);
end
if(nargin>3)
thresh=varargin{3}(:);
end
if(nargin>4)
ratio=varargin{4}(:);
end
%%%% Read the image.
I=imread(input);
%%% if the image is RGB, convert it into grayscale.
if(size(I,3)==3)
I=rgb2gray(I);
end
%%% convert the image to double and display it.
I=im2double(I);
imtool(I);
%%% smoothen the image using gaussian filter.
h=fspecial('gaussian',kernelsize,sigma);
I=imfilter(I,h,'conv');
O=zeros(size(I,1),size(I,2));
% Calculate the gradient using the 7-tap Coefficients given by Farid and
% Simoncelli given in their paper "Differentiation of Discrete
% Multi-Dimensional Signals".
p = [ 0.004711 0.069321 0.245410 0.361117 0.245410 0.069321 0.004711];
d1 = [ 0.018708 0.125376 0.193091 0.000000 -0.193091 -0.125376 -0.018708];
FX=conv2(p,d1,I,'same');
FY=conv2(d1,p,I,'same');
FX=FX.^2;
FY=FY.^2;
FXY=conv2(d1,p,I,'same');
%%%% convolve the gradients with a larger gaussian.
gauss=fspecial('gaussian',ceil(ratio*kernelsize),sigma);
FX=imfilter(FX,gauss,'conv');
FY=imfilter(FY,gauss,'conv');
FXY=imfilter(FXY,gauss,'conv');
%%% each point in the image compute the scalar interest measure and
%%% compare it with specified thresh. If greater then set the
%%% corresponding pixel in output to be 1.
for y=1:1:size(I,1)
for x=1:1:size(I,2)
mat=[FX(y,x),FXY(y,x);FXY(y,x),FY(y,x)];
V=eigs(mat);
lambda1=abs(V(1));
lambda2=abs(V(2));
calc=(lambda1*lambda2)-0.06*(lambda1+lambda2).^2;
if(calc>abs(thresh))
O(y,x)=1;
end
end
end
imshow(O);

 

Tuesday, 15 March 2011

TRAFFIC SIMULATION

TRAFFIC_SIMULATION 
Simulate Traffic at a Light





function traffic ( cycle_num )

%*****************************************************************************80
%
%% TRAFFIC simulates the cars waiting at one traffic light.
%
%
%
%  Parameters:
%
%    Input, integer CYCLE_NUM, the number of 10-second time cycles to model.
%
%  Local Parameters:
%
%    Local, integer CARS, the number of cars waiting at the light.
%
%    Local, integer CARS_IN, the total number of cars that have come.
%
%    Local, integer CARS_OUT, the total number of cars that have left.
%
%    Local, integer CYCLE, the number of time cycles that have elapsed.
%
%    Local, integer CYCLE_LENGTH, the number of seconds in one time cycle.
%
%    Local, integer GREEN_CYCLES, the number of 10-second time cycles that 
%    a green light lasts.
%
%    Local, integer GREEN_TIMER, keeps track of the number of time cycles the
%    green light has been on.
%
%    Local, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Local, real P, the probability that a new car will come to the light
%    in the next second.
%
%    Local, integer RED_CYCLES, the number of 10-second time cycles that 
%    a red light lasts.
%
%    Local, integer RED_TIMER, keeps track of the number of time cycles the
%    red light has been on.
%

%
%  Initialize.
%
  cars = 0;
  cars_in = 0;
  cars_out = 0;
  car_wait_cycles = 0;
  cycle = 0;
  cycle_length = 10;
  green_cycles = 2;
  green_timer = 0;
  light = 'r';
  p = 0.3;
  red_cycles = 4;
  red_timer = 0;
%
%  Set up the plot data.
%
  plot_data = zeros(2,cycle_num+1);
%
%  Handle the "0"-th cycle.
%
  plot_data(1,cycle+1) = cycle;
  plot_data(2,cycle+1) = cars;

  prq ( cars, light, cycle );
%
%  Handle cycles 1 through CYCLE_NUM.
%
  for cycle = 1 : cycle_num
%
%  Each second of the cycle, choose a random number.
%  If it is less than P, then a new car appeared at the light at that second.
%
    r = rand ( cycle_length, 1 );
    cars_new = sum ( r < p );
    cars = cars + cars_new;
    cars_in = cars_in + cars_new;
%
%  Handle this time cycle depending on whether the light is green or red.
%
    if ( light == 'g' )
      [ cars, cars_out, light, green_timer ] = go ( green_cycles, cars, ...
        cars_out, light, green_timer );
    else
      [ cars, light, red_timer ] = stop ( red_cycles, cars, light, red_timer );
    end
%
%  At the end of this cycle, how many cars are waiting?
%
    car_wait_cycles = car_wait_cycles + cars;
%
%  Print the current status.
%
    prq ( cars, light, cycle );

    plot_data(1,cycle+1) = cycle;
    plot_data(2,cycle+1) = cars;

  end

  plot ( plot_data(1,1:cycle_num+1), plot_data(2,1:cycle_num+1) )
  xlabel ( 'Time Cycles' )
  ylabel ( 'Cars Waiting' )
  title ( 'Traffic waiting at a Light' )

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of cycles =       %d\n', cycle_num );
  fprintf ( 1, '  Simulated time =         %d seconds\n', cycle_num * cycle_length );
  fprintf ( 1, '  Number of cars in =      %d\n', cars_in );
  fprintf ( 1, '  Number of cars waiting = %d\n', cars );
  fprintf ( 1, '  Number of cars out =     %d\n', cars_out );
  fprintf ( 1, '  Percentage Out/In = %7.1f%%\n', 100 * cars_out / cars_in );
  wait_average_seconds = car_wait_cycles * cycle_length / cars_in;
  fprintf ( 1, '  Average wait = %7.2f seconds\n', wait_average_seconds );
  wait_average_lights = car_wait_cycles / cars_in / ( red_cycles + green_cycles );
  fprintf ( 1, '  Average wait = %7.2f light cycles\n', wait_average_lights );

  return
end
function [ cars, cars_out, light, green_timer ] = go ( green_cycles, cars, ...
  cars_out, light, green_timer )

%*****************************************************************************80
%
%% GO simulates traffic when the light is green.
%
%
%
%  Parameters:
%
%    Input, integer GREEN_CYCLES, the number of 10-second time cycles that 
%    a green light lasts.
%
%    Input, integer CARS, the number of cars stopped at the light.
%
%    Input, integer CARS_OUT, the total number of cars that have gone
%    through the light.
%
%    Input, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Input, integer GREEN_TIMER, keeps track of the number of time cycles the
%    green light has been on.
%
%    Output, integer CARS, the number of cars stopped at the light.
%
%    Output, integer CARS_OUT, the total number of cars that have gone
%    through the light.
%
%    Output, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Output, integer GREEN_TIMER, keeps track of the number of time cycles the
%    green light has been on.
%

%
%  In one 10-second time cycle, we estimate 8 cars can move out.
%
  cars_through = min ( 8, cars );

  cars = cars - cars_through;
  cars_out = cars_out + cars_through;
%
%  Advance the timer.  If the green light has timed out, reset the timer 
%  and switch to red.
%
  green_timer = green_timer + 1;

  if ( green_cycles <= green_timer )
    light = 'r';
    green_timer = 0;
  end

  return
end
function [ cars, light, red_timer ] = stop ( red_cycles, cars, light, ...
  red_timer )

%*****************************************************************************80
%
%% STOP simulates the traffic when the light is red.
%
%  Parameters:
%
%    Input, integer RED_CYCLES, the number of 10-second time cycles that 
%    a red light lasts.
%
%    Input, integer CARS, the number of cars stopped at the light.
%
%    Input, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Input, integer RED_TIMER, keeps track of the number of time cycles the
%    red light has been on.
%
%    Output, integer CARS, the number of cars stopped at the light.
%
%    Output, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Output, integer RED_TIMER, keeps track of the number of time cycles the
%    red light has been on.
%

%
%  Advance the timer.
%  If the red light has timed out, reset the timer and switch to green.
%
  red_timer = red_timer + 1;

  if ( red_cycles <= red_timer )
    light = 'g';
    red_timer = 0;
  end

  return
end
function prq ( cars, light, cycle )

%*****************************************************************************80
%
%% PRQ prints the current traffic waiting at the light.
%
%  Parameters:
%
%    Input, integer CARS, the number of cars stopped at the light.
%
%    Input, integer LIGHT, the state of the light.
%    'r', the light is now red.
%    'g', the light is now green.
%
%    Input, integer CYCLE, the current 10-second time cycle.
%
  fprintf ( 1, '%4d ', cycle );
  if ( light == 'r' )
    fprintf ( 'R  ' );
  else
    fprintf ( 'G  ' );
  end
  i = cars;
  c = floor ( i / 100 );
  i = i - 100 * c;
  for j = 1 : c
    fprintf ( 'C' );
  end
  x = floor ( i / 10 );
  i = i - 10 * x;
  for j = 1 : x
    fprintf ( 'X' );
  end
  for j = 1 : i
    fprintf ( 'I' );
  end
  fprintf ( 1, '\n' );

  return
end

LinkWithin

Related Posts Plugin for WordPress, Blogger...