• staned
    了解作者
  • matlab
    开发工具
  • 1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-04-01 16:38
    上传日期
提出一种将信号从载波频率下变频到基带的方法
OOK.zip
  • OOK
  • G40318.m
    4.5KB
内容介绍
clear clc close all %% Sine signal analysis %% Parameters Fs=16000; % sampling frequency Ts=1/Fs; % sample period N=Fs; % number of samples t=0:Ts:(N-1)*Ts; % Time axis f=-Fs/2:Fs/N:Fs/2-Fs/N; % Frequency axis [-Fe/2, Fe/2] fc=4e3; % center frequency df=4e3; % Frequency deviation f0= fc-df/2; %f0=2000hz f1= fc+df/2; %f1=6000Hz %% carrier sequence c0=sin(2*pi*f0*t); Npts0=50*floor(Fs/f0); c1=sin(2*pi*f1*t); Npts1=50*floor(Fs/f1); %% plot f0 %time figure stem(t(1:Npts0),c0(1:Npts0)) xlabel('Time (s)') ylabel('Amplitude(V)') title (['Carrier signal with a frequency fi=' num2str(f0) ' Hz' ' , sampled at Fs=' num2str(Fs) ' Hz']) %frequency C0=fftshift(fft(c0))/Fs; % Normalized spectrum computing figure stem(f,abs(C0)) axis([-Fs/2 Fs/2 0 1]) xlabel('Frequency (Hz)') ylabel('Spectrum(Vs)') title (['Carrier signal spectrum']) grid on %% plot f1 %time figure stem(t(1:Npts1),c1(1:Npts1)) xlabel('Time (s)') ylabel('Amplitude(V)') title (['Carrier signal with a frequency fi=' num2str(f1) ' Hz' ' , sampled at Fs=' num2str(Fs) ' Hz']) %frequency C1=fftshift(fft(c1))/Fs; % Normalized spectrum computing figure stem(f,abs(C1)) axis([-Fs/2 Fs/2 0 1]) xlabel('Frequency (Hz)') ylabel('Spectrum(Vs)') title (['Carrier signal spectrum']) grid on %% Digital data bitrate = 500; %[b/s] Nbit = bitrate; % We have a duration of 1s since we are transmitting Fs sample bit_length = Fs/bitrate; % number of samples per bit data = randi([0,1],1,Nbit); %binary sequence data0 = data; data1 = ones(size(data0));%make another sequence which full with 1 for k = 1:Nbit data1(k)=abs(data1(k)-data0(k));%make the binary sequence "0" to "1" and "1" to "0" end ook_sig0 = zeros(size(c0)); ook_sig1 = zeros(size(c1)); for k = 1:Nbit ook_sig0((k-1)*bit_length+1:k*bit_length)= data0(k)*c0((k-1)*bit_length+1:k*bit_length); ook_sig1((k-1)*bit_length+1:k*bit_length)= data1(k)*c1((k-1)*bit_length+1:k*bit_length); end ook_sig0 = ook_sig0 +2; ook_sig1 = ook_sig1 +2; % LED generates positive amplitudes %% plot for f0 %time domain figure plot(t(1:Npts0),ook_sig0(1:Npts0)) xlabel('Time (s)') ylabel('Amplitude(V)') title (['FKS signal with a bitrate R=' num2str(bitrate) ' b/s' ' , sampled at Fs=' num2str(Fs) ' Hz, Int. frequency =' num2str(f0) 'Hz']) %Spectrum FKS0_spect=fftshift(fft(ook_sig0))/Fs; % Normalized spectrum computing figure plot(f,10*log10(abs(FKS0_spect))) axis([-Fs/2 Fs/2 -60 10]) xlabel('Frequency (Hz)') ylabel('Spectrum(Vs)') title (['OOK signal spectrum : bitrate R=' num2str(bitrate) ' b/s' ', sampled at Fs=' num2str(Fs/1e3) ' kHz, Int. frequency =' num2str(f0/1e3) 'kHz']) grid on %% plot for f1 %time domain figure plot(t(1:Npts1),ook_sig1(1:Npts1)) xlabel('Time (s)') ylabel('Amplitude(V)') title (['FKS signal with a bitrate R=' num2str(bitrate) ' b/s' ' , sampled at Fs=' num2str(Fs) ' Hz, Int. frequency =' num2str(f1) 'Hz']) %Spectrum FKS1_spect=fftshift(fft(ook_sig1))/Fs; % Normalized spectrum computing figure plot(f,10*log10(abs(FKS1_spect))) axis([-Fs/2 Fs/2 -60 10]) xlabel('Frequency (Hz)') ylabel('Spectrum(Vs)') title (['OOK signal spectrum : bitrate R=' num2str(bitrate) ' b/s' ', sampled at Fs=' num2str(Fs/1e3) ' kHz, Int. frequency =' num2str(f1/1e3) 'kHz']) grid on %% Remove the DC offset DC0 = mean(ook_sig0); ook_sig0 = ook_sig0 - DC0; DC1 = mean(ook_sig1); ook_sig1 = ook_sig1 - DC1; %% Receiver processing %f0 ook_sig0_down = ook_sig0.*c0; Wp = 2e3*2/Fs; Ws = 2.5e3*2/Fs; Rs = 60; Rp = 3; [n,Wn] = buttord(Wp,Ws,Rp,Rs); [b,a] = butter(n,Wn); %setting a lowpass filter ook_sig0_down = filter(b,a,ook_sig0_down); Data0 = zeros(size(data0)); % the reconstructed binary sequence for k = 1:Nbit if(sum(abs(ook_sig0((k-1)*bit_length+1:k*bit_length)))>0.5) Data0(k) = 1; else Data0(k) = 0; end end %f1 ook_sig1_down = ook_sig1.*c1; Wp = 6e3*2/Fs; Ws = 6.5e3*2/Fs; Rs = 60; Rp = 3; [n,Wn] = buttord(Wp,Ws,Rp,Rs); [b,a] = butter(n,Wn); %setting a lowpass filter ook_sig1_down = filter(b,a,ook_sig1_down); Data1f = zeros(size(data1)); % the reconstructed binary sequence for k = 1:Nbit if(sum(abs(ook_sig1((k-1)*bit_length+1:k*bit_length)))>0.5) Data1f(k) = 1; else Data1f(k) = 0; end end Data1 = ones(size(data1)); for k = 1:Nbit Data1(k)=abs(Data1(k)-Data1f(k)); end Data = Data0 .*Data1; error= (sum(abs(Data-data)))/length(data)
评论
    相关推荐