how can I plot frequency spectrum and impulse response with hold on?

Hi. I passed the signals in the time domain to the frequency domain and plotted the spectrum:
L = length(Ti{i,j}); % Length Of Time & Signal Vectors
Ts = mean(diff(Tt)); % Sampling Interval
Fs{i,j} = 1/Ts; % Sampling Frequency
Fn = Fs{i,j}/2; % Nyquist Frequency
X = fftshift(fft(x)/L);
Fv2 = linspace(-Fn, Fn, L);
plot(Fv2, abs(X));
grid;
xlabel('Frequency (Hz)');
ylabel('Amplitude');
Then I designed a low pass filter and plotted the impulse response of the filter:
[n, fo, ao, w] = firpmord ([0.09 0.7], [1 0], [0.001 0.01], Fs{i,j});
b = firpm (n, fo, ao, w);
filtered_signal{i,j} = filtfilt(b,1,nonoffset{i,j});
fvtool(b,1)
I want to plot the graph of the signal in the frequency domain and the graph of the impulse response. But I need to convert frequency to normalized frequency and convert amplitude to dB. How can I do these? I would be very happy if you show me a way.

 Akzeptierte Antwort

Star Strider
Star Strider am 30 Nov. 2021

0 Stimmen

Use freqz instead of fvtool to plot the transfer function of the filter, and specify the sampling frequency of the signal in the freqz call so that the frequency is plotted as actual frequency, not normalised frequency. It will be necessary to plot the phase angle of ‘X’ as well as the amplitude in the appropriate plots, since freqz produces subplot plots. Note that only the ‘positive’ half of ‘X’ will need to be plotted because the freqz transfer function only depicts the ‘positive’ half of its Fourier transform.
.

8 Kommentare

I didn't fully understand what you said. It draws with freqz and fvtool normalized frequency. Actually what I want to do here is "plot(Fv2, abs(X));" I want to plot its graph with normalized frequency and db magnitude.
I can’t run the code because too much of it is ‘over the horizon’ and invisible.
Should the plot depict the Fourier transform of the signal before and after filtering, or the Fourier transform of the signal co-plotted with the transfer function, or something else (all with respect to the normalised frequency)?
.
My unfiltered signals were in the time domain. Then I converted the unfiltered signals from time domain to frequency domain with the following code.
https://www.mathworks.com/matlabcentral/answers/820855-convert-signal-from-time-domain-to-frequency-domain-with-fft?s_tid=srchtitle
L = length(Ti{i,j}); % Length Of Time & Signal Vectors
Ts = mean(diff(Tt)); % Sampling Interval
Fs{i,j} = 1/Ts; % Sampling Frequency
Fn = Fs{i,j}/2; % Nyquist Frequency
X = fftshift(fft(x)/L);
Fv2 = linspace(-Fn, Fn, L);
plot(Fv2, abs(X));
grid;
xlabel('Frequency (Hz)');
ylabel('Amplitude');
I think I can use ydb = mag2db(y) to make the y-axis dB. But how can I write Hertz as Normalized Frequency (x pi rad/sample) on the x-axis?
I’m still not certain what the desired result is, however converting Hz to rad/s is straightforward. Since the maximum frequency in the Fourier transform is the Nyquist frequency (thank you for quoting my code!) and that corresponds to π rad/sec —
f_radsec = f_Hz * pi / Fn
where ‘f_Hz’ is the frequency vector in Hz and ‘Fn’ is the Nyquist frequency, or similarly here —
f_radsec = f_Hz * pi / f_Hz(end)
I did not test this (since I don’t have the data), however it should work.
.
studentmatlaber
studentmatlaber am 15 Dez. 2021
Bearbeitet: studentmatlaber am 15 Dez. 2021
As a result, what I want is to be able to plot the impulse response graph of the filter and the graph of the unfiltered signal in the frequency domain. So I think I can see where in the signal I'm filtering. Using the transformation you mentioned, I plotted the impulse response graph of the filter with the signal in the frequency domain. Do you think there is an absurdity in the graphic? It's the first time I've done a filtering operation, and I know very little about it. I am very grateful for your help. (cutoff frequencies [0.09 0.7]).
If that is what the desired result is, then it is not absurd. It shows the signal and the filter in the frequency domain, apparently correctly.
The only thing I would consider changing is to make the maximum of the signal equal to 0 dB. The easiest way to do that is to divide the entire signal magnitude by the maximum magnitude.
.
"to divide the entire signal magnitude by the maximum magnitude" It didn't come to life as a code in my head.
f_radsec = Fv2 * pi / Fn; %Hz to rad/sample
fvtool(b,1) %impulse response
hold on
ydb = mag2db(abs(X)); %amplitude to magnitude (dB)
plot(f_radsec, ydb);
What do i need to add? Thank you very much for your time.
As always, my pleasure!
I’m thinking something like this —
XdB0 = @(X) mag2db(abs(X) / max(abs(X)));
w = 0:0.1:pi;
X = 1./sort(randn(32,1) + 1j*randn(32,1));
figure
semilogx(w, mag2db(abs(X)))
hold on
semilogx(w, XdB0(X))
hold off
grid
xlabel('Frequency (rad/s)')
ylabel('Magnitude (dB)')
legend('Original','Normallised To 0 dB', 'Location','best')
The normalisation is not absolutely necessary, however using it plots everything with the same maximum 0 dB reference.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by