Wednesday, 16 March 2011

Conditionally colored line plot

coloring partialy matlab code


Plots vectors x & y using one color for when y is greater than a given threshold value, and another color when y is less than the threshold.


You can optionally specify any standard formatting string that PLOT accepts (eg 'rx:' for a red dotted line with cross markers).


The plotting follows the standard MATLAB PLOT approach of linearly interpolating between data values. The coloring/linestyle changes at y = threshold, even on the interpolated line.

Time division multiplexing


Time division multiplexing in matlab
This program perform TDM for as many signals as you want





function [y]=TDM_nik(x)
% x contains all the signals to be multiplexed
% y is multiplexed signal
%----------------------------------------
% Example: if you have to mutiplex two signals e.g. x1 and x2 (of course both of same length)
% if length is not same append zeros in smaller one to make it equal to larger on
% then make x(1,:)=x1, x(2,x2)...x(r, xr) (if you have r signals to be multiplexed)
% then simple run y=TDM_nik(x)
%-Do it x1=1:10, x2=10:-1:1, x3(1:5)=4, x3(6:10)=-4, x(1,:)=x1, x(2,:)=x2,
% x(3,:)=x3 amd y=TDM_nik(x)
%If you have any problem or feedback please contact me @
%%===============================================
% NIKESH BAJAJ
% Asst. Prof., Lovely Professional University, India
% Almameter: Aligarh Muslim University, India
% +919915522564, bajaj.nikkey@gmail.com
%%===============================================
[r c]=size(x);
k=0;
% Multiplexing
for i=1:c
for j=1:r
k=k+1;
y(k)=x(j,i);
end
end
% Ploting
color='ybrgmkc';
figure(1)
sig='x1';
for i=1:r
sig(2)=i+48;
j=mod(i,7)+1;
subplot(r,1,i)
stem(x(i,:),color(j),'linewidth',2)
title(sig)
ylabel('Amplitude')
grid
end
xlabel('Time')
t=1/r:1/r:c;
figure(2)
for i=1:r
j=mod(i,7)+1;
stem(t(i:r:r*c),y(i:r:r*c),color(j),'linewidth',2)
hold on
grid
end
hold off
title('Time Division Multiplexed Sequence')
xlabel('Time')
ylabel('Amplitude')

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;

LinkWithin

Related Posts Plugin for WordPress, Blogger...