Sie sind auf Seite 1von 10

%% Linear phase filters - preserve shape of a filtered signal

% This is the code used during a youtube video presentation dealing with linear phase filters

% Search for linear phase at http://youtube.com/ddorran

% Code available from https://dadorran.wordpress.com

close all ; clear all; clc

fs = 100;

T = 1/fs; %sampling interval

N = 2000; %length of signal being synthesised

n = 0:N-1; %samples of the signal

t = n*T;

plot_range = [N/2-100:N/2+100];

%% synthesise a signal

x = cos(2*pi*10*t) + 0.5*cos(2*pi*20*t + 1.4);

subplot(2,1,1);

plot(t(plot_range),x(plot_range))

xlabel('Time (seconds)');

ylabel('Amplitude')

title('Synthesised Signals')

axis tight

% Add some noise

ns = randn(1,length(x)+100)*2;
%filter the noise to synthesise band limited noise

[b a] = butter(5, [0.28 0.33],'bandpass');

ns_filtered = filter(b,a,ns);

%add noise to clean signal

x_ns = x +ns_filtered(end-length(x)+1:end);

hold on

noisy_x = plot(t(plot_range), x_ns(plot_range),'r');

legend('clean signal', 'noisy signal')

%% Plot frequency Content of Noisy Signal

subplot(2,1,2)

X_ns = fft(x_ns);

fax = [0:N-1]/(N/2); % normalised frequency axis

plot(fax(1:N/2), abs(X_ns(1:N/2))/(N/2)) ; %plot first half of spectrum

xlabel('frequency ( x \pi rads/sample)')

ylabel('Magnitude')

title('Magnitude Spectrum of Noisy Signal')

%% Filter out the noise using an IIR filter (non-linear phase)

[b_iir a_iir] = cheby1(10, 0.5, [0.27 0.34], 'stop');

y_iir = filter(b_iir,a_iir, x_ns);

[H_iir w] = freqz(b_iir,a_iir); %determine frequency response

subplot(2,1,2);
hold on

plot(w/pi, abs(H_iir),'r')

legend('|X(\omega)|','|H(\omega)|')

pause

Y_iir = fft(y_iir);

plot(fax(1:N/2), abs(Y_iir(1:N/2))/(N/2),'g') ; %plot first half of spectrum

legend('|X(\omega)|','|H(\omega)|','|Y(\omega)|')

pause

subplot(2,1,1)

non_linear_y = plot(t(plot_range),y_iir(plot_range),'g')

legend('clean signal', 'noisy signal','filtered signal')

pause

set(noisy_x,'visible', 'off')

%% Examine the magnitude and phase response of the IIR filter

figure(2)

subplot(2,1,1)

plot(w/pi,abs(H_iir))

xlabel('frequency ( x \pi rads/sample)')

ylabel('Magnitude')

title('Magnitude Response of filter')

subplot(2,1,2)
plot(w/pi,angle(H_iir))

xlabel('frequency ( x \pi rads/sample)')

ylabel('Phase Shift')

title('Phase Response of filter')

%% Now filter using an FIR filter (with linear phase)

b_fir = fir1(100, [0.27 0.34],'stop');

a_fir = 1;

y_fir = filter(b_fir,a_fir, x_ns);

figure(1)

subplot(2,1,1)

plot(t(plot_range),y_fir(plot_range),'k')

legend('clean signal', 'noisy signal','filtered signal (non-linear)','filtered signal (linear)')

[H_fir, w ]= freqz(b_fir,a_fir);

subplot(2,1,2)

plot(w/pi, abs(H_fir),'k')

legend('|X(\omega)|','|H(\omega) Non-linear|','|Y(\omega)|','|H(\omega)| linear')

%% Compare the frequency responses of the two filter design approaches

figure(2)

subplot(2,1,1)
hold on

plot(w/pi,abs(H_fir),'g')

legend('non-linear filter','linear filter')

subplot(2,1,2)

hold on

plot(w/pi,angle(H_fir),'g')

legend('non-linear filter','linear filter')

pause

%% Why does linear phase preserve the shape??

close all

clear all; clc;

fs = 1000;

t = 0:1/fs:2;

x1 = cos(2*pi*3*t-pi/2);

x2 = cos(2*pi*5*t-(pi/2)/3*5);

pause

subplot(3,1,1)

plot(t,x1)

subplot(3,1,2)

plot(t,x2)

subplot(3,1,3)

plot(t,x1+x2,'g')

hold on
DSP

% OFDM Code
% Author: Ihsan Ullah,
% Ms-55 Electrical,
% College of EME,
% NUST Pakistan
% No.of Carriers: 64
% coding used: Convolutional coding
% Single frame size: 96 bits
% Total no. of Frames: 100
% Modulation: 16-QAM
% No. of Pilots: 4
% Cylic Extension: 25%(16)
close all
clear all
clc
%%
% Generating and coding data
t_data=randi(9600,1)';
x=1;
si=1; %for BER rows
%%
for d=1:100;
data=t_data(x:x+95);
x=x+96;
k=3;
n=6;
s1=size(data,2); % Size of input matrix
j=s1/k;
%%
% Convolutionally encoding data
constlen=7;
codegen = [171 133]; % Polynomial
trellis = poly2trellis(constlen, codegen);
codedata = convenc(data, trellis);
%%
%Interleaving coded data
s2=size(codedata,2);
j=s2/4;
matrix=reshape(codedata,j,4);
intlvddata = matintrlv(matrix',2,2)'; % Interleave.
intlvddata=intlvddata';
%%
% Binary to decimal conversion
dec=bi2de(intlvddata','left-msb');
%%
%16-QAM Modulation
M=16;
y = qammod(dec,M);
% scatterplot(y);
%%
% Pilot insertion
lendata=length(y);
pilt=3+3j;
nofpits=4;
k=1;
for i=(1:13:52)

pilt_tdata1(i)=pilt;
for j=(i+1:i+12);
pilt_data1(j)=y(k);
k=k+1;
end
end
pilt_data1=pilt_data1'; % size of pilt_data =52
pilt_data(1:52)=pilt_data1(1:52); % upsizing to 64
pilt_data(13:64)=pilt_data1(1:52); % upsizing to 64
for i=1:52

pilt_data(i+6)=pilt_data1(i);

end
%%
% IFFT
ifft_sig=ifft(pilt_data',64);
%%
% Adding Cyclic Extension
cext_data=zeros(80,1);
cext_data(1:16)=ifft_sig(49:64);
for i=1:64

cext_data(i+16)=ifft_sig(i);

end
%%
% Channel
% SNR
o=1;
for snr=0:2:50
ofdm_sig=awgn(cext_data,snr,'measured'); % Adding white Gaussian Noise
% figure;
% index=1:80;
% plot(index,cext_data,'b',index,ofdm_sig,'r'); %plot both signals
% legend('Original Signal to be Transmitted','Signal with AWGN');
%%
% RECEIVER
%%
%Removing Cyclic Extension
for i=1:64

rxed_sig(i)=ofdm_sig(i+16);

end
%%
% FFT
ff_sig=fft(rxed_sig,64);
%%
% Pilot Synch%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:52

synched_sig1(i)=ff_sig(i+6);

end
k=1;
for i=(1:13:52)

for j=(i+1:i+12);
synched-sig(k=synched_sig1(j);
k=k+1;
end
end
% scatterplot(synched_sig)
%%
% Demodulation
dem_data= qamdemod(synched_sig,16);
%%
% Decimal to binary conversion
bin=de2bi(dem_data','left-msb');
bin=bin';
%%
% De-Interleaving
deintlvddata = matdeintrlv(bin,2,2); % De-Interleave
deintlvddata=deintlvddata';
deintlvddata=deintlvddata(:)';
%%
%Decoding data
n=6;
k=3;
decodedata =vitdec(deintlvddata,trellis,5,'trunc','hard'); % decoding
datausing veterbi decoder
rxed_data=decodedata;
%%
% Calculating BER
rxed_data=rxed_data(:)';
errors=0;
c=xor(data,rxed_data);
errors=nnz(c);
% for i=1:length(data)
%
%
% if rxed_data(i)~=data(i);
% errors=errors+1;
%
% end
% end
BER(si,o)=errors/length(data);
o=o+1;
end % SNR loop ends here
si=si+1;
end % main data loop
%%
% Time averaging for optimum results
for col=1:25; %%%change if SNR loop Changed
ber(1,col)=0;
for row=1:100;

ber(1,col)=ber(1,col)+BER(row,col);
end
end
ber=ber./100;
%%
figure
i=0:2:48;
semilogy(i,ber);
title('BER vs SNR');
ylabel('BER');
xlabel('SNR (dB)');
grid on
DSP

% Ultra Easy FBMC Modulator


% Conservatoire National des Arts et M�tiers
% Michel Terr�
% January 2014
% FBMC modulator and demodulator, using the Phydyas prototype filter
% Ultra Easy approach avoiding polyphase filters
% N=16, 16 subcarriers
% Frame, Number of "FBMC symbols"
clear all;
close all;
clc;
N=16;
% Prototype Filter (cf M. Bellanger, Phydyas project)
H1=0.971960;
H2=sqrt(2)/2;
H3=0.235147;
factech=1+2*(H1+H2+H3);
hef(1:4*N)=0;
for i=1:4*N-1
hef(1+i)=1-2*H1*cos(pi*i/(2*N))+2*H2*cos(pi*i/N)-2*H3*cos(pi*i*3/(2*N));
end
hef=hef/factech;
%
% Prototype filter impulse response
h=hef;
figure(1);
plot(h);
title ('Phydyas Filter Impulse response');
% Initialization for transmission
Frame=4;
y=zeros(1,4*N+(Frame-1)*N/2);
s=zeros(N,Frame);
for ntrame=1:Frame
% OQAM Modulator
if rem(ntrame,2)==1
s(1:2:N,ntrame)=sign(randn(N/2,1));
s(2:2:N,ntrame)=j*sign(randn(N/2,1));
else
s(1:2:N,ntrame)=j*sign(randn(N/2,1));
s(2:2:N,ntrame)=sign(randn(N/2,1));
end
x=ifft(s(:,ntrame));
% Duplication of the signal
x4=[x.' x.' x.' x.'];
% We apply the filter on the duplicated signal
signal=x4.*h;
% Transmitted signal
y(1+(ntrame-1)*N/2:(ntrame-1)*N/2+4*N)=y(1+(ntrame-1)*N/2:(ntrame-1)*N/2+4*N)+signal;
end
% Demodulator
sdemod=zeros(N,Frame);
for ntrame=1:4
r=y(1+(ntrame-1)*N/2:(ntrame-1)*N/2+4*N).*h; % We apply the filter
u=zeros(1,N);
for k=1:4
u=u+r(1,1+(k-1)*N:k*N); % Summation
end
u=u.';
sest=fft(u);
sest=sest/0.6863; % Normalization

% OQAM demodulation
if rem(ntrame,2)==1
sdemod(1:2:N,ntrame)=real(sest(1:2:N));
sdemod(2:2:N,ntrame)=j*imag(sest(2:2:N));
else
sdemod(1:2:N,ntrame)=j*imag(sest(1:2:N));
sdemod(2:2:N,ntrame)=real(sest(2:2:N));
end
end
figure(2)
plot(s,'or');
hold on
plot(sdemod,'xk');
title('FBMC OQAM, o transmitted, x received');

Das könnte Ihnen auch gefallen