0

I am trying to generate 100 packets(row vectors each of size 8192) of random bits(1,-1),filter them using butterworth filter and then plot their average power spectral density. I have to do this using MATLAB.My o/p should be a filtered sinc with a very sharp peak. When I use this code for smaller sized packets say 100 it works. But for 8192 it doesn't. I want someone to review my code for errors please.

%generates a random square matrix of 8192x8192
n=rand(8192);
%initiates a row vector of 64 zeros
B=zeros(1,64);
%makes a butterworth(lowpass) filter
[num,den]=butter(20,.6); 
%two for loops to generate 100 row vectors(packets) each of size 8192 that
%give 1 for any value greater than 0.5 and vice versa
for c=1:100
for k=1:8192
if n(c,k)>=0.5
n(c,k)=1;
else
n(c,k)=-1;
end
%filter the generated vectors and calculate average power spectral density
x=filter(num,den,n(c,:));
A=fftshift(fft(x,64));
psd=A.*conj(A);
B=B+psd;
end
end
plot(B./100)
xlabel 'Frequency', ylabel 'Average Power Spectral Density'
Samia
  • 1
  • 4

2 Answers2

0

I saw two things:

First: I think the second end should be in a different position:

%generates a random square matrix of 8192x8192
n=rand(8192);

%initiates a row vector of 64 zeros
B=zeros(1,64);
%makes a butterworth(lowpass) filter
[num,den]=butter(20,.6); 
%two for loops to generate 100 row vectors(packets) each of size 8192 that
%give 1 for any value greater than 0.5 and vice versa
for c=1:100
    for k=1:8192
        if n(c,k)>=0.5
            n(c,k)=1;
        else
            n(c,k)=-1;
        end
    end
        %filter the generated vectors and calculate average power spectral density
        x=filter(num,den,n(c,:));
        A=fftshift(fft(x,64));
        psd=A.*conj(A);
        B=B+psd;

end
plot(B./100)
xlabel 'Frequency', ylabel 'Average Power Spectral Density'

Second: It takes forever to get the +1 and -1 values using your code. You should use the indexing operators that Matlab provides:

%generates a random square matrix of 8192x8192
n=rand(8192);
n(n>=0.5)=1;
n(n<0.5)=-1;

%initiates a row vector of 64 zeros
B=zeros(1,64);
%makes a butterworth(lowpass) filter
[num,den]=butter(20,.6); 
%two for loops to generate 100 row vectors(packets) each of size 8192 that
%give 1 for any value greater than 0.5 and vice versa
for c=1:100
        %filter the generated vectors and calculate average power spectral density
        x=filter(num,den,n(c,:));
        A=fftshift(fft(x,64));
        psd=A.*conj(A);
        B=B+psd;

end
plot(B./100)
xlabel 'Frequency', ylabel 'Average Power Spectral Density'
phyrox
  • 2,423
  • 15
  • 23
0

Take absolute values of the filtered data

Replace

 x=filter(num,den,n(c,:));

with

x=abs(filter(num,den,n(c,:)));

gives a sharp peak that can be seen here

Arulkumar
  • 12,966
  • 14
  • 47
  • 68