⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wireless tips and tricks.m

📁 Matlab初学者的好东东 里面包括了一些编程常用的范例
💻 M
字号:
%Wireless tips and tricks-JC-2/23/06
%To run hit F5
%I've decided to produce this list of wireless tips and tricks to benefit the
%first time or seldom user of Matlab interested in wireless
%and communication design. Most of the elements of a wireless or communications system
%are provided here. You can use these to design and analyze almost any 
%communications system. I know how time consuming it was for me to figure out
%how to do things since I don't have any of the specialized toolboxes. A good benefit
%of not having the toolboxes and doing it on your own gives a better understanding of
%key points that may be missed using the toolboxes. There may be a list of
%these somewhere else but I have never found it. The best way to do this is
%use comments and the user can copy and paste to the command window or to
%an m-file or just remove the semicolons and observe the command window.
%I will try to add enough information as to use. Some are quite obvious, but if you
%draw a blank on how to do something, it can be quite frustrating. I don't believe
%one should be spending undo time figuring out how to use Matlab but the time should be
%used learning concepts and I hope this helps to some degree. 
%======================================================================
%Serial to parallel and parallel to serial conversion of bit vector
%======================================================================
data_in=[1 0 1 1 0 1 0 1]
%datain =
     %1     0     1     1     0     1     0     1
data_odd=data_in(1:2:length(data_in))%odd
%data_odd =
 %    1     1     0     0
 data_even=data_in(2:2:length(data_in))%even
%data_even =
 %    0     1     1     1
 data_out=[data_odd;data_even]%notice semicolon
 %data_out =
   % 1     1     0     0
   % 0     1     1     1
 data_parserout=data_out(1:end)
 %data_parser =
     %1     0     1     1     0     1     0     1
%=========================================================================    
 %Differentially encode a bit sequence    
%==========================================================================
data_diff=[0];
for n=1:length(data_in)
    data_diff=[data_diff,xor(data_in(n),data_diff(n))];
end
data_diff1=data_diff
%data_in =
     %1     0     1     1     0     1     0     1
%data_diff1 =
     %0     1     1     0     1     1     0     0     1
%==========================================================================    
 %Change bit sequence from 0 1 1 . . . to +/- ones    
%==========================================================================
data_diff2=sign(data_diff1-.5)%to +/-1
%data_diff2 =
    %-1     1     1    -1     1     1    -1    -1     1
data_diff2=data_diff2>0%change back to 0/1
%data_diff2 =
    % 0     1     1     0     1     1     0     0     1
%==========================================================================    
%Make and plot constellation for QPSK for 45, 135, 225, and 315 degrees 
%and BPSK for 0 and 180 degrees
%==========================================================================    
qpsk = exp(j*[pi/4 3*pi/4 5*pi/4 7*pi/4]);
bpsk = exp(j*[0 pi]);
figure(1)
plot(qpsk,'o'); xlabel('Real');ylabel('Imag'); % plot the constellation
axis([-2 2 -2 2]);title('QPSK Constellation');
grid on;
figure(2)
plot(bpsk,'o'); xlabel('Real');ylabel('Imag'); % plot the constellation
axis([-2 2 -2 2]);title('BPSK Constellation');
grid on;
%==========================================================================
%Attenuater,gain,capacitor,rectifier,multiplier(balanced modulator),limiter,squarewave
%squaring circuit,inverter,I,Q(90 degree) phase shifter
%==========================================================================
%.7*signal(attenuator)
%1.7*signal(gain)
%signal=signal-mean(signal)%removes DC component-capacitor
%signal(signal<0)=0(rectifier)
%signal.*carrier(balanced modulator-multiplier)
%signal(signal<0)=-1,signal(signal>0)=1(limiter)
%square wave%0 to 1 volt peak or +/- 1 volts p-p
   %sinewave(x>0)=1
   %sinewave(x<0)=-1
%signal.*signal(squaring circuit)
%-signal(inverter)(180 degree phase shifter)
%cos(signal),sine(signal)(I,Q(90 degree phase shifter)
%==============================================================
%Map bits to phase for QPSK using a Gray code configuration
%==============================================================
clear
rand('state',0);%keeps data sequence constant
% The first step is a generation of the bit sequence
Nbits=20;
data=round(rand(1,Nbits)) 
symbols=[];
for ii=1:2:Nbits
twobit=data(ii:ii+1); % words of 2 bits
if twobit == [0 0 ] % conversion by comparison
phase=pi/4;%45 degrees
elseif twobit == [0 1];
phase=3*pi/4;%135 degrees
elseif twobit == [ 1 1];
phase=5*pi/4;%225 degrees
elseif twobit == [ 1 0];
phase=7*pi/4;%315 degrees
end
symbols=[symbols phase]; % new phase added to sequence
end
%show constellation
qpsk1=exp(j*symbols);
figure(3)
plot(qpsk1,'o');
xlabel('Real');ylabel('Imag');
axis([-2 2 -2 2]);title('QPSK Constellation,Gray coding');
grid on;

%=====================================================================
%Filters-low pass, bandpass
%======================================================================
%I have constructed m files at the author (CJ) page at Mathworks which show
%how to design low pass and bandpass filters using z transforms. You can
%design these filters at about any frequency you need. This should get you
%there.
%http://www.mathworks.com/matlabcentral/fileexchange/loadAuthorIndex.do
%=======================================================================
%Binary to Gray convertor
%=======================================================================
%function g = bi2gray( b )
% BI2GRAY converts a binary code to a Gray code. The most
% significat bit is the left hand side bit. 
% When the input argument is a binary matrix, each row is
% converted to the Gray code.
%
% See also: GRAY2BI at Mathworks
%
% Programmer-adrian@ubicom.tudelft.nl
b=[1 1 -1 1 1 -1 -1 -1];
b=([1 1 0 1 1 0 0 0]);%+/- to 0 1 
    % copy the msb:
    g(:,1) = b(:,1);
        for i = 2:size(b,2),
        g(:,i) = xor( b(:,i-1), b(:,i) ); 
    end
   Gray=g
   %Gray =
    % 1     0     1     1     0     1     0     0
%Use of a Gray code(map two bits per symbol to Gray code) in a QPSK system
%can improve the BER by approx. 3dB.
%========================================================================
%Pseudo random code generator
%========================================================================     
N = 100;		        % Number of symbols
fs = 4*1e3;		        % Sampling frequency
Ts = 1/fs;	            % Sampling time = 1/fs
T = 1/100;		        % Symbol time
randn('state',0);       % Keeps PRBS from changing on reruns
t = [0:Ts:N*T-Ts];      % Time vector

symbols = sign(randn(N,1))';%generate N random binary symbols, +/- 1(notice transpose')
symbols1 = ones(T/Ts,1)*symbols;	
s2 = symbols1(:); 	%PRBS @ +/-1
%plot PRBS sequence
figure(4)
plot(t,s2)
axis([0 max(t) -1.2 1.2])
xlabel('                                            time');
grid on;
title('PRBS')
%========================================================================
%One bit flash analog to digital(ADC) convertor or comparator
%=========================================================================
clear
fs=1e5;%sampling frequency
fc=2e2; % carrier frequency
t=(0:1/fs:1e-1);
Fn=fs/2;
% cosine wave
x = cos(2*pi*fc*t );
pt1=1.7e-5;%sets level where threshhold device comparator triggers
H=5;%(volts)
L=0;%(volts)
LEN=length(x);
for ii=1:LEN;
    if x(ii)>=pt1;%output(x) going above pt1 threshold setting
        pv2(ii)=H;%pulse voltage
    else;
        pv2(ii)=L;
    end;
end 
po1=pv2;%pulse out=pulse voltage
%plot ADC output
figure(5)
plot(t,po1)
axis([0,.1 -1,6]);
xlabel('time');
title('ADC out');
grid on;
%=====================================================================
%Spectrum analyzer-You need a good spectrum analyzer to analyze your
%system. This is a little long but gives you what you would see in the lab.
%=====================================================================
clear
fs=1e6;%sampling frequency
fc=2e3; % carrier frequency
t=(0:1/fs:1e-1);
Fn=fs/2;
% cosine wave
x=cos(2*pi*fc*t);
x1=x;
NFFX1=2.^(ceil(log(length(x1))/log(2)));
FFTX1=fft(x1,NFFX1);
NumUniquePts=ceil((NFFX1+1)/2); 
FFTX1=FFTX1(1:NumUniquePts);
MX1=abs(FFTX1);
MX1=MX1*2;
MX1(1)=MX1(1)/2;
MX1(length(MX1))=MX1(length(MX1))/2;
MX1=MX1/length(x1);
f=(0:NumUniquePts-1)*2*Fn/NFFX1;
%plot spectrum analyzer output
figure(6); plot(f,20*log10(MX1));xlabel('FREQUENCY');ylabel('20LOG10=DB');
grid on;
axis([1600 2400 -15 0]);
title('Spectrum analyzer output');
%====================================================================
%Phase shift or delay
%====================================================================
clear
fs=100000;%sampling frequency
fc=2e3; % carrier frequency
t=(0:1/fs:.0015);
x=cos(2*pi*fc*t);%cosine wave
figure(7)
plot(x,'g');
grid on
hold on
title('Phase shift or delay')
phase_shift=45*(pi/180);%phaseshift(radians)180deg=3.14,90=1.57,45=.785
x=cos(2*pi*fc*t + phase_shift);
plot(x,'r')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -