I am having trouble getting from Frequency (bins) to (Hz) with the graphs that i am plotting
6 次查看(过去 30 天)
显示 更早的评论
Here is a code that i am working on. it requires me to create a filter using the FDA tool and then input a signal to test it. iv done most of it but i am having trouble converting the bins to Hz. what methods are there that could work with my code? and how do i apply them?
figure;
t = 0:1/10000:5;
signal = chirp(t,0,50,50000);
out = filter(bandstop,signal);
subplot(2,1,1);
plot(abs(signal));
xlabel('Frequency (Hz)');
subplot (2,1,2);
plot(abs(out));
xlabel('Frequency (Hz)');
%fft
figure;
subplot (1,1,1);
FF = 20*log(abs(fft(out)));
plot(FF);
xlabel('Frequency (Hz)');
% Original + noise
figure;
subplot(2,1,1);
N = awgn(signal,10);
plot (abs(N));
xlabel('Frequency (Hz)');
out2 = filter(bandstop,N);
subplot (2,1,2);
plot (abs(out2));
xlabel('Frequency (Hz)');
%New fft(noise)
figure;
subplot (1,1,1);
plot (20*log(abs(fft(S))));
xlabel('Frequency (Hz)');
function Hd = bandstop
%BANDSTOP PROJECT Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB® 8.3 and the Signal Processing Toolbox 6.21.
% Generated on: 23-Oct-2015 14:19:52
% FIR least-squares Bandstop filter designed using the FIRLS function.
% All frequency values are in kHz. Fs = 140; % Sampling Frequency
N = 14; % Order
Fpass1 = 22; % First Passband Frequency
Fstop1 = 26; % First Stopband Frequency
Fstop2 = 34; % Second Stopband Frequency
Fpass2 = 38; % Second Passband Frequency
Wpass1 = 1; % First Passband Weight
Wstop = 75; % Stopband Weight
Wpass2 = 1; % Second Passband Weight
% Calculate the coefficients using the FIRLS function.
b = firls(N, [0 Fpass1 Fstop1 Fstop2 Fpass2 Fs/2]/(Fs/2), [1 1 0 0 1 ... 1], [Wpass1 Wstop Wpass2]);
Hd = dfilt.dffir(b);
0 个评论
回答(1 个)
Star Strider
2015-11-21
The current R2015b documentation for fft is not as clear as in previous versions. The code between the first two plot figures in the fft documentation in the R2015a (and earlier) versions is much clearer and more straightforward in my opinion.
Some example code to plot the unfiltered signal is:
t = 0:1/10000:5;
signal = chirp(t,0,50,50000);
L = length(t); % Length Of Signal
Ts = 1E-4; % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Fsignal = fft(signal)*2/L; % Scaled FFT For Plotting
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector For Plotting
Iv = 1:length(Fv); % Index Vector (Makes Plotting Easier)
figure(1)
plot(Fv, abs(Fsignal(Iv)))
grid
I didn’t take the log or do anything else, since I simply want to illustrate the most efficient code to calculate and plot the fft of your signal. I didn’t see that you created a frequency vector for your plot, so this code corrects that problem.
2 个评论
Star Strider
2015-11-21
My pleasure.
To plot the amplitude in dB, just change the plot call to:
figure(1)
plot(Fv, 20*log10(abs(Fsignal(Iv))))
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude (dB)')
You have to use the log10 — not log — function to calculate dB, because that is how it it defined.
Do the same for your filtered signals. You have already defined ‘L’, ‘Ts’, ‘Fs’, ‘Fn’, ‘Fv’, and ‘Iv’, so you can just refer to them in your plots of the filtered signals that you can create by cutting and pasting the code for figure(1) and changing Fsignal to the other signals you want to plot.
If you are plotting the one-sided fft, be sure to include the scaling and normalising factor 2/L in all your fft assignments:
Fsignal = fft(signal)*2/L; % Scaled FFT For Plotting
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!