DSPRelated.com

Frequency Sweep WAV Creator

Ron March 25, 2011 Coded in Matlab
function [] = SineSweep(fstart, fstop, length, method, fs, bits, PHI)
%SINESWEEP Creates a custom sine wave sweep.
%   SineSweep(fstart, fstop, length, [method], [fs], [bits], [PHI])
%   fstart (Hz) - instantaneous frequency at time 0
%   fstop (Hz) - instantaneous frequency at time length
%   length (s) - the length of time to perform the sweep
%   method - 'quadratic', 'logarithmic', or 'linear' (default)
%   fs (Hz) - the sampling frequency (default = 48KHz)
%   bits - bit depth of each sample 8, 16 (default), 24, or 32
%   PHI (deg) - initial phase angle. cos = 0, sin = 270 (default)
%
%   Written by: Ron Elbaz
%   Date: February 15, 2011

%check and set missing parameters
if exist('method','var') ~= 1
    method = 'linear';
end
if exist('fs','var') ~= 1
    fs = 48000;
end
if exist('bits','var') ~= 1
    bits = 16;
end
if bits ~= 8 && bits ~= 16 && bits ~= 24 && bits ~= 32
    bits = 16;
end
if exist('PHI','var') ~= 1
    PHI = 270;
end

%make sure start and stop frequencies are valid
if fstop < fstart
    temp = fstart;
    fstart = fstop;
    fstop = temp;
end

%avoid aliasing
if fstart > fs/2
    fstart = fs/2;
end
if fstop > fs/2
    fstop = fs/2;
end

%create time vector
t = 0:(1/fs):length;

%scale to avoid clipping due to rounding error
Y = 0.9999*chirp(t,fstart,length,fstop, method, PHI);

%play the sound
% sound(Y,fs);

%store sweep to wav file
wavwrite(Y,fs,bits,'SineSweep.wav');

end

Differential Phase Shift Keying -

Senthilkumar March 24, 2011 Coded in Scilab
//Generation of Differential Phase shift keying signal
clc;
bk = [1,0,1,1,0,1,1,1];//input digital sequence
for i = 1:length(bk)
  if(bk(i)==1)
    bk_not(i) =~1;
  else
    bk_not(i)= 1;
  end
end
dk_1(1) = 1&bk(1);  //initial value of differential encoded sequence
dk_1_not(1)=0&bk_not(1);
dk(1) = xor(dk_1(1),dk_1_not(1))//first bit of dpsk encoder
for i=2:length(bk)
  dk_1(i) = dk(i-1);
  dk_1_not(i) = ~dk(i-1);
  dk(i) = xor((dk_1(i)&bk(i)),(dk_1_not(i)&bk_not(i)));
end
for i =1:length(dk)
  if(dk(i)==1)
    dk_radians(i)=0;
  elseif(dk(i)==0)
    dk_radians(i)=%pi;
  end
end
disp(bk,'(bk)')
bk_not = bk_not';
disp(bk_not,'(bk_not)')
dk = dk';
disp(dk,'Differentially encoded sequence (dk)')
dk_radians = dk_radians';
disp(dk_radians,'Transmitted phase in radians')

Unipolar NRZ

Senthilkumar March 24, 2011 Coded in Scilab
//Nonreturn-to-zero unipolar format: Discrete PAM Signals Generation
//Unipolar NRZ 
clear;
close;
clc;
x = [0 1 0 0 0 1 0 0 1 1];
binary_zero = [0 0 0 0 0 0 0 0 0 0];
binary_one = [1 1 1 1 1 1 1 1 1 1];
L = length(x);
L1 = length(binary_zero);
total_duration = L*L;
//plotting
a =gca();
a.data_bounds =[0 -2;L*L1 2];
for i =1:L
  if(x(i)==0)
    plot([i*L-L+1:i*L],binary_zero);
    poly1= a.children(1).children(1);
    poly1.thickness =3;
  else
    plot([i*L-L+1:i*L],binary_one);
    poly1= a.children(1).children(1);
    poly1.thickness =3;
  end
end
xgrid(1)
title('Unipolar NRZ')

A-Law Compression - non uniform Quantization

Senthilkumar March 24, 20111 comment Coded in Scilab
function [Cx,Xmax] =  Alaw(x,A)
  //Non-linear Quantization
  //A-law: A-law nonlinear quantization
  //x = input vector
  //Cx = A-law compressor output
  //Xmax = maximum of input vector x
  Xmax  = max(abs(x));
  for i = 1:length(x)
    if(x(i)/Xmax < = 1/A)
       Cx(i) = A*abs(x(i)/Xmax)./(1+log(A));
    elseif(x(i)/Xmax > 1/A)
      Cx(i) = (1+log(A*abs(x(i)/Xmax)))./(1+log(A));
    end
  end
  Cx = Cx/Xmax; //normalization of output vector
  Cx = Cx';
endfunction

u -Law and Inverse u-Law

Senthilkumar March 24, 2011 Coded in Scilab
function [Cx,Xmax] =  mulaw(x,mu)
  //Non-linear Quantization
  //mulaw: mulaw nonlinear quantization
  //x = input vector
  //Cx = mulaw compressor output
  //Xmax = maximum of input vector x
  Xmax  = max(abs(x));
  if(log(1+mu)~=0)
    Cx = (log(1+mu*abs(x/Xmax))./log(1+mu));
  else
    Cx = x/Xmax;
  end
  
  Cx = Cx/Xmax; //normalization of output vector
endfunction

////////////////////////////////////////////////////
function x =  invmulaw(y,mu)
  //Non-linear Quantization
  //invmulaw: inverse mulaw nonlinear quantization
  //x = output vector 
  //y = input vector (using mulaw nonlinear comression)
  x = (((1+mu).^(abs(y))-1)./mu)  
endfunction

Uniform Quantization - PCM

Senthilkumar March 24, 2011 Coded in Scilab
function [SQNR,xq,en_code] = uniform_pcm(x,L)
  //x = input sequence
  //L = number of qunatization levels 
 xmax = max(abs(x));
xq = x/xmax;
en_code = xq;
d = 2/L;
q = d*[0:L-1];
q = q-((L-1)/2)*d;
for i = 1:L
    xq(find(((q(i)-d/2)<= xq)&(xq<=(q(i)+d/2))))=...     
    q(i).*ones(1,length(find(((q(i)-d/2)<=xq)&(xq<=(q(i)+d/2)))));
    en_code(find(xq == q(i)))= (i-1).*ones(1,length(find(xq == q(i))));
end
  xq = xq*xmax;
  SQNR = 20*log10(norm(x)/norm(x-xq));
endfunction

PCM Encoding-Converting Quantized decimal sample values in to binary

Senthilkumar March 24, 2011 Coded in Scilab
function [c] = PCM_Encoding(x,L,en_code)
  //Encoding: Converting Quantized decimal sample values in to binary
   //x = input sequence
   //L = number of qunatization levels 
   //en_code = normalized input sequence
n = log2(L);
c = zeros(length(x),n);
for i = 1:length(x)
  for j = n:-1:0
    if(fix(en_code(i)/(2^j))==1)
      c(i,(n-j)) =1;
      en_code(i) = en_code(i)-2^j;
    end
  end
end
disp(c)

PseudoNoise Sequence Generator

Senthilkumar March 24, 20114 comments Coded in Scilab
//PN sequence generation
//Maximum-length sequence generator
//Program to generate Maximum Length Pseudo Noise Sequence
clc;
//Assign Initial value for PN generator
x0= 1;
x1= 0;
x2 =0;
x3 =0;
N = input('Enter the period of the signal')
for i =1:N
  x3 =x2;
  x2 =x1;
  x1 = x0;
  x0 =xor(x1,x3);
  disp(i,'The PN sequence at step')
  x = [x1 x2 x3];
  disp(x,'x=')
end
m = [7,8,9,10,11,12,13,17,19];
N = 2^m-1;
disp('Table Range of PN Sequence lengths')
disp('_________________________________________________________')
disp('Length of shift register (m)')
disp(m)
disp('PN sequence Length (N)')
disp(N)
disp('_________________________________________________________')

Logical XOR - not available inh scilab 5.2

Senthilkumar March 24, 20111 comment Coded in Scilab
function [value] = xor(A,B)
  if(A==B)
    value = 0;
  else
    value = 1;
  end
endfunction

Hamming Weight and Hamming Distance

Senthilkumar March 24, 20111 comment Coded in Scilab
// Hamming Weight and Hamming Distance
//H(7,4)
//Code Word Length = 7, Message Word length = 4, Parity bits =3
close;
clc;
//Getting Code Words
code1 = input('Enter the first code word');
code2 =  input('Enter the second code word');
Hamming_Distance = 0;
for i = 1:length(code1)
  Hamming_Distance =Hamming_Distance+xor(code1(i),code2(i));
end
disp(Hamming_Distance,'Hamming Distance')
//Result
//Enter the first code word [0,1,1,1,0,0,1]
//Enter the second code word[1,1,0,0,1,0,1]
//Hamming Distance       4.