# Help with matlab ( contructing Low pass filter and apply it to square wave.

Discussion in 'Programmer's Corner' started by ynaji, Feb 6, 2008.

1. ### ynaji Thread Starter Member

Jan 28, 2007
10
0

The Assignement is construct a Low pass filter RC using magnitude transfer function calculated H=1/squareroot(1+(f/fo)^2). apply the filter to the square wave. the Frequency is 10 KHz. Examine the spectral output. I tried and I gave up . Any ideas What I am doing wrong ?

n = 0:255;
t = n/256;
f1 = 10000;
omega = 2*pi*f1;
x = cos(omega*t);
subplot(3,3,1);
plot(t,x)
xlabel('time (s)')
ylabel('amplitude')
title('Sine waive x(t)')
X=fft(x)/256;
subplot(3,3,2);
plot(0:63, abs(X(1:64)))
xlabel('frequency (HZ)')
ylabel('amplitud')
title('Spectrum X')
n = 0:255;
t = n/256;
x = [ ones(1,64),-ones(1,64), ones(1,64), -ones(1,64)];
subplot(3,3,3);
plot(t,x);
X=fft(x)/256;
f=0:255;
fo=10000;
H= (1/(sqrt(1+(f/fo)^2)));
H(1:3)= ones(1,3);
H(254:256)= ones(1,3);
Y= H.*X;
subplot(3,3,4);
plot(t,Y)

2. ### Dave Retired Moderator

Nov 17, 2003
6,960
145
You cannot use the mpower function (implicitly) on a matrix in the way you are doing. You must saddle it with a for-loop:

Code ( (Unknown Language)):
1. n = 0:255;
2. t = n/256;
3. f1 = 10000;
4. omega = 2*pi*f1;
5. x = cos(omega*t);
6. subplot(3,3,1);
7. plot(t,x)
8. xlabel('time (s)')
9. ylabel('amplitude')
10. title('Sine waive x(t)')
11. X=fft(x)/256;
12. subplot(3,3,2);
13. plot(0:63, abs(X(1:64)))
14. xlabel('frequency (HZ)')
15. ylabel('amplitud')
16. title('Spectrum X')
17. n = 0:255;
18. t = n/256;
19. x = [ ones(1,64),-ones(1,64), ones(1,64), -ones(1,64)];
20. subplot(3,3,3);
21. plot(t,x);
22. X=fft(x)/256;
23. f=0:255;
24. fo=10000;
25. for i=1:(size(f,2))
26.     H(i) = (1/(sqrt(1+(f(i)/fo)^2)));
27. end
28. H(1:3)= ones(1,3);
29. H(254:256)= ones(1,3);
30. Y= H.*X;
31. subplot(3,3,4);
32. plot(t,Y)
Dave

3. ### ynaji Thread Starter Member

Jan 28, 2007
10
0
Thank you very much .

4. ### ynaji Thread Starter Member

Jan 28, 2007
10
0
Thank you very much dave for your response. but I have another question regarding matlab code. When I created Square wave, I have to put 10 Khz frequency not 2 cycle.