Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.66 KB | None | 0 0
  1. % lab17_ex_airplane.m
  2.   clear all; close all;
  3.   m=128; cm_plasma=plasma(m); cm = plasma;   % color maps for gray printing
  4.  
  5. % Read a recorded IQ signal - two VOR avionics signals
  6.  %  FileName = 'SDRSharp_Airplane_112500kHz_IQ.wav'; T=5; demod=3 ; fc = 2.9285e+5;
  7.   FileName = 'SDRSharp_Airplane_112849kHz_IQ.wav'; T=5; demod=3; fc = -5.5353e+4;
  8.  
  9. % #########################################################################
  10. % START - From program lab16_ex_IQ_DFT.m ##################################
  11.   inf = audioinfo(FileName), pause                % what is "inside"
  12.   fs = inf.SampleRate;                            % sampling rate
  13.   if(T==0) [x,fs] = audioread(FileName);          % read the whole signal
  14.   else     [x,fs] = audioread(FileName,[1,T*fs]); % read only T seconds
  15.   end                                             %
  16.   whos, pause                                     % what is in the memory
  17.   Nx = length(x),                                 % signal length
  18.  
  19. % Reconstruct the complex-value IQ data, if necessary add Q=0
  20.   [dummy,M] = size(x);
  21.   if(M==2) x = x(:,1) - j*x(:,2); else x = x(1:Nx,1) + j*zeros(Nx,1); end
  22.    
  23.  
  24. % Parameters - lengths of FFT and STFT, central signal sample
  25.   Nc = floor( Nx/2 ); Nfft = min(2^17,2*Nc); Nstft = 512;
  26.  
  27. % Power Spectral Density (PSD) of the signal
  28.   n = Nc-Nfft/2+1 : Nc+Nfft/2;             % FFT length, samples for analysis
  29.   df = fs/Nfft;                            % df - step in frequency
  30.   f = df * (0 : 1 : Nfft-1);               % frequency axis [ 0, fs ]
  31.   fshift = df * (-Nfft/2 : 1 : Nfft/2-1);  % frequency axis [ -fs/2, fs/2 ]
  32.   w = kaiser(Nfft,10);                     % window function used
  33.   X = fft( x(n) .* w );                    % DFT of windowed signal
  34.   P = 2*X.*conj(X) / (fs*sum(w.^2));       % Power Spectral Density (dB/Hz)
  35.   Pshift = fftshift( P );                  % circularly shifted PSD
  36.  
  37.   % Parameters for Short Time Fourier Transform (STFT) of the signal
  38.   N = Nstft; df = fs/N; ff = df*(0:1:N-1);  ffshift = df*(-N/2:1:N/2-1);
  39.  
  40.       figure(2)
  41.       subplot(211); plot(f,10*log10(abs(P))); xlabel('f (HZ)'); ylabel('(dB/Hz)')
  42.       axis tight; grid; title('PSD for frequencies [0-fs)');
  43.       subplot(212); spectrogram(x(n),kaiser(Nstft,10),Nstft-Nstft/4,ff,fs);
  44.       colormap(cm); pause
  45.  
  46.       figure(3)
  47.       subplot(211); plot(fshift,10*log10(abs(Pshift))); xlabel('f (HZ)'); ylabel('(dB/Hz)')
  48.       axis tight; grid; title('PSD for frequencies [-fs/2, fs/2)');
  49.       subplot(212); spectrogram(x(n),kaiser(Nstft,10),Nstft-Nstft/4,ffshift,fs);
  50.       colormap(cm); pause
  51.       subplot(111);
  52.  
  53.  % STOP - From program lab16_ex_IQ_DFT.m ##################################
  54.  % ########################################################################
  55.  
  56.  if(demod==3) % airplane azimuth decoding using VOR signal
  57.  
  58.     M = 501; M2=(M-1)/2;           % filter length
  59.     fam = 25000; dt=1/fam;         % frequency width of AM modulation around fc
  60.     f1 = fc-fam/2; f2 = fc+fam/2;  % Band-Pass filter h design
  61.     h = cfirpm(M-1,[-fs/2 (f1-df) (f1+df) (f2-df) (f2+df) fs/2]/(fs/2),@bandpass);
  62.     x = conv(x,h); x=x(M:end-M+1);                       % Band-pass filtration of VOR
  63.     x = sqrt( real(x).*real(x) + imag(x).*imag(x) );     % AM demodulation
  64.    
  65.     X = fft( x(n) .* w );                    % DFT of windowed signal
  66.     P = 2*X.*conj(X) / (fs*sum(w.^2));       % Power Spectral Density (dB/Hz)
  67.     Pshift = fftshift( P );                  % circularly shifted PSD
  68.     figure(4);
  69.     subplot(211);
  70.     plot(fshift,10*log10(abs(Pshift)));  xlabel('f (HZ)'); ylabel('(dB/Hz)');
  71.     title('PSD of AM demodulated');
  72.     subplot(212);
  73.     spectrogram(x(n),kaiser(Nstft,10),Nstft-Nstft/4,ffshift,fs);
  74.       colormap(cm);
  75.    
  76.     x = decimate(x,round(fs/fam)); % [U,D] = rat(fs/fam); x = resample( x, U, D );
  77.     x = x - mean(x);               % mean subtraction
  78.  
  79.     if(0) % only for verification test
  80.        N = length(x); t=dt*(0:length(x)-1);
  81.        x = sin(2*pi*30*t) + cos(2*pi*(9960*t-480/(2*pi*30)*cos(2*pi*30*t)));
  82.        x = x';
  83.     end    
  84.     xc = x; % make a copy
  85.  
  86.   % Low-pass fitration of 30 Hz azimuth signal
  87.     hLP30 = fir1(M-1,50/(fam/2),'low');      % filter coeffs design
  88.     x = conv(x,hLP30); x = x(M2+1:end-M2);   % filtering
  89.     x = x - mean(x);
  90.     x_azim = x(2:end-1)/max(x);  % 2,-1 due to finst computation
  91.  
  92.   % Extraction of 30 Hz signal with reference phase
  93.     hBP10k = fir1(M-1,[9000,11000]/(fam/2),'bandpass'); % BP filter design
  94.     x = conv(xc,hBP10k); x=x(M2+1:end-M2);   % separation of FM component
  95.     x = unwrap(angle (hilbert(x)));       % angle calculation
  96.     x = x(3:end)-x(1:end-2);                 % 3-point difference
  97.     x = (1/(2*pi))*x/(2*dt);                 % f instantaneous
  98.    %  x = hilbert(x);
  99.    %  x = (1/(2*pi))*angle( x(3:end).*conj( x(1:end-2)) )/(2*dt); %x =[x 0];
  100.     % x = (1/(2*pi))*(real(x(2:end-1)).*(imag(x(3:end))-imag(x(1:end-2)))-...
  101.     % imag(x(2:end-1)).*(real(x(3:end))-real(x(1:end-2))) ) / (2*dt);
  102.  
  103.     figure(5);
  104.     plot(x);title('Noisy finst(t) 9,96 kHz');xlabel('t[s]'); ylabel('f(Hz)');
  105.     x = conv(x,hLP30); x=x(M2+1:end-M2);     % LP filtration / denoising
  106.    
  107.     x = x - mean(x);                         % remove mean value            
  108.     x_ref = x/max(x);                        % normalize amplitude to 1
  109.     figure(6);
  110.     plot(x_azim);xlabel('t[s]');
  111.     title('30Hz signals: azimuth(solid), ref(dashed)');
  112.     hold on
  113.     plot(x_ref,'--')
  114.  
  115.   % Phase shift estimation  
  116.     phi_inst = angle( hilbert(x_azim).*conj(hilbert(x_ref)) );
  117.     figure(7);
  118.    
  119.     plot(phi_inst); title('phi inst(t)');
  120.     xlabel('t[s]');
  121.     ylabel('(rad)');
  122.     phi_estim = mean( phi_inst )
  123.  
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement