To view the corresponding frequency values in descending order of amplitude, you can sort the indices of the amplitude values (peak_ft) and use those indices to reorder the corresponding frequency values (peak_fr). This can be done by sorting the indices based on the amplitude and then using those indices to reorder the frequency values.
Here’s how you can do it:
Modified Code to Sort Frequencies Corresponding to Amplitudes:
% Sort amplitude values into descending order and get the corresponding frequency
[DescendAmp, sortIdx] = sort(peak_ft, 'descend'); % Sort amplitudes and get sorting indices
% Reorder the frequencies using the sorted indices
DescendFreq = peak_fr(sortIdx);
% Now DescendAmp contains the sorted amplitudes, and DescendFreq contains the corresponding frequencies
% You can plot the results or display them
% Plot the corresponding frequency vs. amplitude in descending order
figure;
plot(DescendFreq, DescendAmp, 'o-');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Sorted Frequency vs. Amplitude');
grid on;
Explanation of the Changes:
-
Sorting Amplitude and Getting Indices:
[DescendAmp, sortIdx] = sort(peak_ft, 'descend');- This line sorts the amplitude values in descending order and returns the sorting indices
sortIdx, which tell you how to reorder the original data.
-
Reordering the Frequencies:
DescendFreq = peak_fr(sortIdx);- Using
sortIdx, we reorder the frequencies (peak_fr) to match the sorted amplitudes.
-
Plotting the Results:
plot(DescendFreq, DescendAmp, 'o-');- This line creates a plot showing the sorted frequencies against their corresponding amplitudes.
Output:
DescendAmp: Amplitudes sorted in descending order.DescendFreq: Frequencies corresponding to the sorted amplitudes.
Now, you can directly view the frequencies corresponding to the peaks in descending order of their amplitudes.
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: