2011年3月24日 星期四

Demo of aliasing for Matlab

% demo of aliasing    2011/3/23  作者:一心の流

 close all;
 clear all;
 clear tt_sf;
 DC=2;                                         %直流偏壓
 phi= 2*pi/360*270.03;               %相移270.03degree;
 freq = 2500000;                         %freq=fo=發射頻率
 samp_freq = 2000000;               %freq=fo=發射頻率
 n=fix(freq/samp_freq);
 freq_nyquist=2*freq;
 if samp_freq < freq_nyquist
    freq_alias= freq - n*samp_freq;
 else
    freq_alias=0;
 end;
 time_base=100000000;                 %單位10000 point/per second
 time_sweep=0.0001;                      %單位second

%------------------------------------------------------------
%  theta=(1:100)*(2*pi)/100;         %2π之區間分成100等分,單位:徑度;
%------------------------------------------------------------
%0~ 2之區間分成2000等分,單位:時間(sec);
% t=linspace(0,1,8); 注意其前後之數值均包括在內的八個點,應為切成七等份才對。0, 0.1429, 0.2857, 0.4286, 0.5714, 0.7143, 0.8571, 1.0000
 t=linspace(0,time_sweep,time_sweep*time_base+1);
 % t_sp=linspace(0,time_sweep,samp_freq*time_sweep+1);
 t_sp=(0:1/samp_freq:time_sweep- 1/samp_freq);
%------------------------------------------------------------
% 公式SR(t)=a+cos(2*pi*fo*t+ phi)   fo=發射頻率
%     SD[k]=a+cos(2*pi*fo*k*Tsp+ phi),是一個data string
% 計算 DC+cos((2*pi)*freq*t+phi)
%------------------------------------------------------------
 SR=DC+cos(2*pi*freq*t+phi);                            %模擬接收訊號 SR(t)=a+cos(2*pi*fo*t+ phi)  
 SD=DC+cos(2*pi*freq*t_sp+phi);                      %模擬接收後取樣訊號 SD[k]=a+cos(2*pi*fo*k*Tsp+ phi),是一個data string
%figure,plot(t, SR, 'b',t_sp, SD, 'm',t_sp, SD, 'ro');
%xlabel('Angle(x), sec with delay 72 degree');       %水平座標名稱
%ylabel('cos(x)');                                                  %垂直座標名稱
% grid on;                                                             %打開格線模式
%------------------------------------------------------------
% 公式Sd[k]= a+cos(2*pi*freq*t);
% 計算 DC+cos((2*pi)*freq*t)
%------------------------------------------------------------
 y2=DC+cos(2*pi*freq*t);
 y4=DC+cos(2*pi*freq*t_sp);
%figure,plot(t,y2,'b',t_sp, y4, 'm',t_sp, y4, 'ro'), hold on;
% figure,plot(t, SR, 'b',t,y2,'g',t_sp, SD, 'r',t_sp, y4, 'm'), hold on;
%xlabel('Angle(x), sec ');        %水平座標名稱
%ylabel('cos(x)');                    %垂直座標名稱
% grid on;                               %打開格線模式
%------------------------------------------------------------
% 計算正交調變信號
% 公式 RI[k]= sin(2*pi*f_al*k*Tsp)=sin(pi*k/2);
%      RQ[k]= cos(2*pi*f_al*k*Tsp)=cos(pi*k/2);
% 公式 MI[k]= RI[k]* SD[k]= sin(2*pi*f_al*k*Tsp)*(a+cos(2*pi*fo*k*Tsp+ phi))
%      MQ[k]= RQ[k]* SD[k]= sin(2*pi*f_al*k*Tsp)*(a+cos(2*pi*fo*k*Tsp+ phi))

%------------------------------------------------------------
RI=sin(2*pi*freq_alias*t_sp);
RQ=cos(2*pi*freq_alias*t_sp);
MI=RI.*SD;                           %%對應元素相乘
MQ=RQ.*SD;                        %%對應元素相乘
%figure,plot(t,SR,'k',t_sp, SD,'m',t_sp, SD,'ro',t_sp, MI,'bo',t_sp, MQ, 'r*',t_sp, MI,'b',t_sp, MQ, 'r'), hold on;
sum_MI=sum(MI);                      %將MI向量的元素相加之結果
sum_MQ=sum(MQ);                   %將MQ向量的元素相加之結果
phi_theta_radians = atan(-(sum_MI/sum_MQ));
phi_theta_degree = phi_theta_radians/(2*pi)*360;
%-----------------------------------------------------------------
% 計算phase unwraping,做相位修正
% sin   cos
%  +    +   -->  計算
%-----------------------------------------------------------------
if (-sum_MI>0 && sum_MQ<0)
    phi_theta_degree=phi_theta_degree+180;
end;
if (-sum_MI<0 && sum_MQ<0)
    phi_theta_degree=phi_theta_degree+180;
end;
if (-sum_MI<0 && sum_MQ>0)
    phi_theta_degree=phi_theta_degree+360;
end;   
disp(phi_theta_degree);
figure,plot(t,SR,'k',t_sp, SD,'m',t_sp, SD,'ro',t_sp, RI,'bo',t_sp, RQ, 'b*'), hold on;
xlabel('Angle(x), sec with delay 72 degree');                  %水平座標名稱
ylabel('cos(x)');                                                            %垂直座標名稱
grid on;                                                                        %打開格線模式

Aliasing

In statistics, signal processing, and related disciplines, aliasing is an effect that causes different continuous signals to become indistinguishable (or aliases of one another) when sampled. When this happens, the original signal cannot be uniquely reconstructed from the sampled signal.


Plot showing aliasing of a Under Sampled 1D sinusoidal signal. The blue dots are the digital samples taken to record the red signal. Clearly they are not enough to reconstruct the original signal: from then, only the blue wrong signal can be interpolated.

Aliasing can take place either in time, temporal aliasing, or in space, spatial aliasing. It may give rise to moiré patterns (when the original image is finely textured) or jagged outlines (when the original has sharp contrasting edges, e.g. screen fonts). ( Wikipedia).

Image Aliased image.

See more examples in → Anti Aliasing .
See also Critical Sampling Distance and Nyquist Rate .

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。