%% 单一频率信号调制
clc
clear
fs = 48000;
fc=18000;
t=0:1/fs:10;
signal = cos(2*pi*fc*t);
[ mr ] = plot_fft( signal,fs,'r' );
figure
[S,F,T,P,FC,TC] = spectrogram(signal,512,256,1024,fs,'yaxis');%20181206 ultragesture
SS = abs(S);
SSS = mapminmax(SS,0,1);
A=P./abs(S);
S1=S;
im=imagesc(T,F,abs(S1).^2);axis tight;
colormap('hot')
[S,F,T,P,FC,TC] = spectrogram(signal,1024,512,1024,fs,'yaxis');%20181206 ultragesture
%% OFDM信号调制
% UltraGesture 中的BC code调制方式
clc
clear
barker= comm.BarkerCode('Length',13,'SamplesPerFrame',13);
trainSeq1 = mapminmax(step(barker),-1,1);
% trainSeq =[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1];
trainSeq=(trainSeq1(:,1))';
trainSeq = [trainSeq trainSeq zeros(1,14)];
y2=trainSeq;
y1 = interp(y2, 12);
% fc=18500;
% fs=48000;
% t=0:1/fs:(length(y1)-1)/fs;
% y = sqrt(2)*cos(2*pi*fc*t);
%
% y =
plot(y1)
% b=zeros(1,168);
% y=[y1 b];
y=y1;
figure
plot(y)
fs=48000;
y=repmat(y,1,2000);
fc = 19000;
t=0:1/fs:(length(y)-1)/fs;
% t=t(1:end-1);
y = sqrt(2)*y.*cos(2*pi*fc*t);
y=mapminmax(y,-1,1);
[ mr ] = plot_fft( y,fs,'r' );
% audiowrite('ultragesture.wav',y,fs);
% addpath E:\声音小组\LQPHP\audiotrack\matlab_code
% n=13;
n=9;
Wn=[17000/(fs/2) 21000/(fs/2)] %设计Butterworth低通滤波器
[a,b]=butter(n,Wn);
y1=y';
y31= filter(a,b,y1);
mr=plot_fft(y31,fs,'b');
figure
% [S,F,T,P,FC,TC] = spectrogram(y31,1024,512,1024,fs,'yaxis');%20181206 ultragesture
[ SS,SSS,S,F,T,P,FC,TC,A ] = STFT1( y31 );
t=0:1/fs:((length(y31)-1)/fs);
y3=mapminmax(y31',-1,1);
%% chirp信号调制
clc
clear
fs=48000;
fc=17000;
t=0:1/fs:10;
tt1=2;
tchirp = 0:1/fs:tt1;
tchirp = tchirp(1:end-1);
yy= chirp(tchirp,16000,tt1,23000);
%使用汉明窗对包络整形
N=length(yy);
h=hamming(N);
for m=1:N
b(m) = yy(m)*h(m);
end
yy=b;
y1=zeros(1,0.05*fs);
y=[yy y1];
ss = [y y y y y y y y y y y y y];
% ss=[ss ss ss ss ss ss ss ss ss ss ss ss];
% ss=[ss ss ss ss ss ss ss ss ss ss ss ss];
%
% t=t(1:end-1);
% A=0.4;
% signal1 = A*cos(2*pi*fc*t);
mr=plot_fft(ss,fs,'b');
% 画STFT图
figure
[S,F,T,P,FC,TC] = spectrogram(ss,512,256,1024,fs,'yaxis');%20181206 ultragesture
SS = abs(S);
SSS = mapminmax(SS,0,1);
A=P./abs(S);
S1=S;
im=imagesc(T,F,abs(S1).^2);axis tight;
colormap('hot')
附代码中用到的画FFT和STFT图的代码:
FFT:
function [ mr ] = plot_fft( x,fs,co )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
N=length(x);
% N=512;
f=(1:N).*fs/(N);
mr=abs(fft(x,N));
plot(f(1:N/2),mr(1:N/2)*2/N,co)
% xlim([16000 23000]);
% ylim([-0.01 0.01])
% hold on
%plot(f,mr,'b')
title('原始信号频谱');
end
画STFT的图:
function [ SS,SSS,S,F,T,P,FC,TC,A ] = STFT1( x )
%UNTITLED 此处显示有关此函数的摘要
% 此处显示详细说明
fs=48000;
SNR=10;
colorbar;
xlabel('Time (Seconds)');ylabel('Hz')
% [S,F,T,P,FC,TC] = spectrogram(x,2048,1024,4096,fs,'yaxis');%20181205
% [S,F,T,P,FC,TC] = spectrogram(x,2048,1024,2048,fs,'yaxis');%20181206 conchirp
[S,F,T,P,FC,TC] = spectrogram(x,1024,512,1024,fs,'yaxis');%20181206 ultragesture
SS = abs(S);
SSS = mapminmax(SS,0,1);
A=P./abs(S);
S1=S;
% for i=2:size(S,2)
% S1(:,i) = S(:,i)-S(:,i-1);
% end
im=imagesc(T,F,abs(S1).^2);axis tight;
% AA=mapminmax(AA,0,1);
% im=imagesc(T,F,AA_gaus.^2);axis tight;
% im=imagesc(T,F,(P./abs(S)));axis tight;
% im=imagesc(T,F,10*log10(abs(P)));axis tight;
%
colormap('hot')
end