이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Help with time variation graphs versus time
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782405/image.png)
댓글 수: 1
채택된 답변
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782460/image.png)
추가 답변 (2개)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1782445/image.png)
Hi @Jose Martinez ,
You mentioned,”Can you help me on how I can obtain this type of graphs (see example), which represent amplitude variations at a certain frequency with respect to time. Note that the Y axis is in logarithmic.“
Please see my response to your comments below. Please see example code snippet provided below. It generates a spectrogram from your dataset, which includes frequency and amplitude values.
% Sample data formatted as cell array data = { '2010/01/01 00:00', 0.109864, 2.04021; '2010/01/01 00:00', 0.122071, 2.8937; '2010/01/01 00:00', 0.134278, 2.84502; '2010/01/01 00:00', 0.146485, 2.92267; '2010/01/01 00:00', 0.158692, 3.11156; '2010/01/01 00:00', 0.170899, 3.41533; '2010/01/01 00:00', 0.183107, 3.10193; '2010/01/01 00:00', 0.195314, 3.32969; '2010/01/01 00:00', 0.207521, 3.29483; '2010/01/01 00:00', 0.219728, 3.21573 };
% Extracting time and amplitude time = datenum(data(:,1)); % Convert date strings to serial date numbers amplitude = cell2mat(data(:,3)); % Amplitude column as numeric
% Convert amplitude to logarithmic scale (in dB) log_amplitude = 20 * log10(amplitude);
% Define parameters for spectrogram window = hamming(5); % Window length (number of samples) noverlap = 2; % Number of overlapping samples nfft = max(256,2^nextpow2(length(window))); % FFT points
% Create the spectrogram [s,f,t] = spectrogram(log_amplitude, window, noverlap, nfft);
% Plotting figure;
% Spectrogram subplot(2,1,1); % Create subplot for spectrogram imagesc(t, f, abs(s)); % Use abs(s) to get magnitude for visualization axis xy; % Flip y-axis for correct orientation xlabel('Time (Years)'); ylabel('Frequency (Hz)'); title('Spectrogram'); colorbar; % Add color bar to indicate amplitude levels set(gca,'YScale','log'); % Set Y-axis to logarithmic scale
% Update x-axis ticks to show years (example) xticks(datenum({'2009-12-31', '2010-06-30', '2011-12-31'})); % Example ticks xticklabels({'2009', '2010', '2011'});
% Waterfall Plot subplot(2,1,2); % Create subplot for waterfall plot waterplot(s,f,t); % Call the provided function to create the waterfall plot
% Function to create waterfall plot of spectrogram function waterplot(s,f,t) waterfall(f,t,abs(s)'.^2); % Transpose s for correct orientation set(gca,'XDir','reverse','View',[30 50]); % Set view angle xlabel("Frequency (Hz)"); ylabel("Time (s)"); title('Waterfall Plot'); end
Let me break down the code step-by-step to understand how it meets yours requirements and to clarify any potential improvements.
Data Preparation
The first part of the code initializes the sample data as a cell array. This format allows for mixed data types, which is useful for handling date strings alongside numeric values.
data = { '2010/01/01 00:00', 0.109864, 2.04021; '2010/01/01 00:00', 0.122071, 2.8937; ... '2010/01/01 00:00', 0.219728, 3.21573 };
Extracting Time and Amplitude
The code extracts the time and amplitude from the dataset. The datenum function converts date strings into serial date numbers, which MATLAB can process for time-based plotting. The amplitude values are converted from a cell array to a numeric array using cell2mat.
time = datenum(data(:,1)); % Convert date strings to serial date numbers amplitude = cell2mat(data(:,3)); % Amplitude column as numeric
Logarithmic Transformation
To meet the user's requirement of displaying the Y-axis in a logarithmic scale, the amplitude values are transformed into decibels (dB) using the formula 20 * log10(amplitude. This transformation is crucial for visualizing amplitude variations effectively.
log_amplitude = 20 * log10(amplitude);
Spectrogram Parameters
The code defines parameters for the spectrogram, including the window length, overlap, and the number of FFT points. The hamming window is commonly used for spectral analysis due to its favorable properties in reducing spectral leakage.
window = hamming(5); % Window length (number of samples) noverlap = 2; % Number of overlapping samples nfft = max(256,2^nextpow2(length(window))); % FFT points
Generating the Spectrogram
The spectrogram function computes the spectrogram of the logarithmic amplitude data. It returns the complex values of the spectrogram, along with frequency and time vectors.
[s,f,t] = spectrogram(log_amplitude, window, noverlap, nfft);
For more information on this function, please refer to
https://www.mathworks.com/help/signal/ref/spectrogram.html
Plotting the Spectrogram
The code creates a figure with two subplots: one for the spectrogram and another for a waterfall plot. The spectrogram is visualized using imagesc, which displays the magnitude of the spectrogram. The Y-axis is set to a logarithmic scale using set(gca,'YScale','log'), fulfilling the user's requirement.
subplot(2,1,1); % Create subplot for spectrogram imagesc(t, f, abs(s)); % Use abs(s) to get magnitude for visualization axis xy; % Flip y-axis for correct orientation xlabel('Time (Years)'); ylabel('Frequency (Hz)'); title('Spectrogram'); colorbar; % Add color bar to indicate amplitude levels set(gca,'YScale','log'); % Set Y-axis to logarithmic scale
Customizing the X-Axis
The code customizes the x-axis ticks to represent years, enhancing the readability of the time axis.
xticks(datenum({'2009-12-31', '2010-06-30', '2011-12-31'})); % Example ticks xticklabels({'2009', '2010', '2011'});
Waterfall Plot
The second subplot is a waterfall plot, which provides a three-dimensional view of the spectrogram data. The waterplot function is defined to create this visualization.
subplot(2,1,2); % Create subplot for waterfall plot waterplot(s,f,t); % Call the provided function to create the waterfall plot
Please see attached
![](/matlabcentral/answers/uploaded_files/1782510/IMG_7998.jpeg)
![](/matlabcentral/answers/uploaded_files/1782505/IMG_7997.jpeg)
In nutshell, this code generates a spectrogram that represents amplitude variations at specific frequencies over time, with the Y-axis displayed in a logarithmic scale. The transformation of amplitude to a logarithmic scale, along with the appropriate plotting functions, makes sure that your requirements are met. If you wish to further refine the spectrogram or customize the visual output, you may consider adjusting the window length, overlap, or the frequency range displayed.
Hope this helps resolve your problem. Please let me know if you have any further questions.
참고 항목
카테고리
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)