1.
fs=25*10^6;
fc=2*10^6;
N=63;
wn=fc*2/fs;
b1=fir1(N-1,wn);
m1=20*log10(abs(fft(b1)));
x=[0:(fs/length(m1)):fs/2];
figure(1)
subplot(211)
plot(b1);
xlabel('n');ylabel('h(n)');
subplot(212)
plot(x,m1(1:length(x)));
c=b1/max(abs(b1));
d=round(c*511);
m2=20*log10(abs(fft(d)));
x2=[0:(fs/length(m2)):fs/2];
figure(2)
subplot(211)
plot(d);
xlabel('n');ylabel('h(n)');
subplot(212)
plot(x2,m2(1:length(x2)));
f1=1*10^6;
f2=3*10^6;
f3=4*10^6;
t=0:1/fs:399/fs;
s1=sin(2*pi*f1*t);
s2=sin(2*pi*f2*t);
s3=sin(2*pi*f3*t);
s=s1+s2+s3;
xh=0:(fs/length(s)):fs/2;
y1=filter(b1,1,s);
figure(3)
subplot(211)
plot(xh,abs(fft(s(1:length(xh)))));
subplot(212)
plot(xh,abs(fft(y1(1:length(xh)))));
figure(4)
subplot(211)
plot(t,s);
subplot(312)
plot(t,y1);
2.
fs=2000*10^3;
f1=30*10^3;
fc1=200*10^3;
f2=30*10^3;
fc2=500*10^3;
t=0:1/fs:1023/fs;
s1=cos(2*pi*f1*t).*cos(2*pi*fc1*t);
s2=cos(2*pi*f2*t).*cos(2*pi*fc2*t);
s=s1+s2;
a=abs(fft(s,1024));
a1=abs(fft(s1,1024));
a2=abs(fft(s2,1024));
xh=0:(fs/length(s)):fs/2;
xh1=0:(fs/length(s1)):fs/2;
xh2=0:(fs/length(s2)):fs/2;
figure(1)
subplot(311)
plot(t,s);title('合成信号时域波形');xlabel('时间(s)');ylabel('幅度(v)');
subplot(312)
plot(t,s1);title('第一路信号时域波形');xlabel('时间(s)');ylabel('幅度(v)');
subplot(313)
plot(t,s2);title('第二路信号时域波形');xlabel('时间(s)');ylabel('幅度(v)');
figure(2)
subplot(311)
plot(xh,a(1:length(xh)));title('合成信号频域波形');xlabel('频率(Hz)');ylabel('幅度(db)');
subplot(312)
plot(xh1,a1(1:length(xh1)));title('第一路信号频域波形');xlabel('频率(Hz)');ylabel('幅度(db)');
subplot(313)
plot(xh2,a2(1:length(xh2)));title('第二路信号频域波形');xlabel('频率(Hz)');ylabel('幅度(db)');
N=16;
fh=300*10^3;
wh=fh*2/fs;
w=blackman(N);
b=fir1(N-1,wh,w);
y=filter(b,1,s);
yfft=abs(fft(y,1024))
xfft=0:(fs/length(y)):fs/2;
figure(3)
subplot(211)
plot(t,y);title('滤波后信号时域波形');xlabel('时间(s)');ylabel('幅度(v)');
subplot(212)
plot(xfft,yfft(1:length(xfft)));title('滤波后信号频域波形');xlabel('频率(Hz)');ylabel('幅度(db)');
figure(4)
freqz(b,1,1024,2*10^6);
s=s/max(abs(s));
da=round(s*2047);
N=12;
fid=fopen('d:\data.txt','w');
for i=1:length(da)
da1=dec2bin(da(i)+(da(i)<0)*2^N,N)
for j=1:N
if da1(j)=='1'
tb=1;
else
tb=0;
end
fprintf(fid,'%d',tb);
end
fprintf(fid,'\r\n');
end
fprintf(fid,':');
fclose(fid);
fid=fopen('d:\data.coe','w');
fprintf(fid,'RADIX=10;\r\n');
fprintf(fid,'coefdata=\r\n');
for i=1:length(da)
if(i<length(da))
fprintf(fid,'%d,\r\n',da(i));
else
fprintf(fid,'%d;\r\n',da(i));
end
end
fclose(fid);
fid=fopen('d:\rom_start.coe','w');
fprintf(fid,'RADIX=10;\r\n');
fprintf(fid,'coefdata=\r\n');
for i=1:length(da)
if(i<length(da))
fprintf(fid,'%d,\r\n',da(i));
else
fprintf(fid,'%d;\r\n',da(i));
end
end
fclose(fid);