Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
fir1 design for filter
hello, i wrote a program to eliminate a random frequensy, its a highpass filter .In a frequency set, first it randomly chose a frequency subsets, then creat a sinusial signal with these frequency components, after this i want to eliminate the smalest frequency in this subsets, my program attenuates the smaller frequencies but doesnt attenuates just the smalest component, how can i do this? my program is this:
f=[100 200 300 400 500 600 700 800];
rand_Number1= uint8(4 + (rand(1) * 4)); index1=randint(rand_Number1,1,[1,8]); for i=1:rand_Number1 Frquency_components(i)=f(index1(i)); end; bigest_freqNU=0;smalest_freqNU=0; for i=1:rand_Number1 if(Frquency_components(i)==max(Frquency_components)) bigest_freqNU= bigest_freqNU+1; end; if(Frquency_components(i)==min(Frquency_components)) smalest_freqNU=++1; end; end;
Fs=8e3; %Specify Sampling Frequency Ts=1/Fs; %Sampling period. Ns=512; %Number of time samples to be plotted. t=[0:Ts:Ts*(Ns-1)]; %Make time array that contains Ns elements
s=zeros(1,Ns); for i=1:rand_Number1 xx=sin(2*pi*Frquency_components(i)*t); s=s+xx; end; x=s; grid on; N=16; %FIR1 requires filter order (N) to be EVEN
st_mi=max(Frquency_components); w_ma=st_mi/(Fs/2); w_mi=st_mi/(Fs/2)-0.1;
W=[w_mi w_ma];
B=fir1(N,W,'high'); B
A=1; freqz(B,A); . pause; figure; subplot(2,1,1); Npts=200; plot(t(1:Npts),x(1:Npts)) title('Time Plots of Input and Output'); xlabel('time (s)'); ylabel('Input Sig');
y = filter(B,A,x); subplot(2,1,2); plot(t(1:Npts),y(1:Npts)); xlabel('time (s)'); ylabel('Filtered Sig'); pause; figure; subplot(2,1,1); xfftmag=(abs(fft(x,Ns))); xfftmagh=xfftmag(1:length(xfftmag)/2);
z=[1:1:length(xfftmagh)]*Fs/Ns; %Make freq array that varies from
plot(z,xfftmagh); %Plot frequency spectrum of input signal title('Input and Output Spectra'); xlabel('freq (Hz)'); ylabel('Input Spectrum'); subplot(2,1,2); yfftmag=(abs(fft(y,Ns))); yfftmagh=yfftmag(1:length(yfftmag)/2);
plot(z,yfftmagh); %Plot frequency spectrum of input signal xlabel('freq (Hz)');