- wdencmp - https://www.mathworks.com/help/wavelet/ref/wdencmp.html
- ddencmp - https://www.mathworks.com/help/wavelet/ref/ddencmp.html
convert signals to spectrogram
8 ビュー (過去 30 日間)
古いコメントを表示
Hi, I want to convert the existing signals to spectrogram images automatically and save them in jpg format, so how do I change the following code to execute the code correctly?
Thank you!
clc
clear all
close all
Path = 'IS\*.mat';
Files = dir(Path);
for i=1:length(Files)
fn = [Path(1:end-5) Files(i,1).name];
load(fn);
y = val(1:3600);
[thr,sorh,keepapp] = ddencmp('den','wv',y);
sig1 =wdencmp('gbl',y,'sym10',10,thr,sorh,keepapp);
fs=128;
pspectrum(sig1,fs,'spectrogram','TimeResolution',0.5)
title('pspectrum Signal')
temp=[num2str(i,'%04d')];
saveas(temp,'jpg');
end
0 件のコメント
回答 (1 件)
ag
2025 年 2 月 5 日 18:04
Hi de,
To convert signals to spectrogram images and save them as JPG files, adjust your MATLAB script to handle file paths and image saving correctly. Use fullfile to construct file paths robustly. Load each .mat file, assuming val contains the signal, and process the desired portion. Denoise the signal using "ddencmp" and "wdencmp". Set the sampling frequency and use pspectrum to generate the spectrogram.
Use figure('Visible', 'off') to create figures invisibly and saveas to save each spectrogram as a JPG in a directory like Spectrograms. Ensure this directory exists by using "mkdir('Spectrograms')" before the loop. Close each figure with "close(gcf)" after saving to manage memory efficiently. This automates converting multiple signals to spectrogram images without manual intervention.
The below code snippet demonstrates the key changes in the code:
% Define the path to the .mat files
Path = 'IS\*.mat';
Files = dir(Path);
% Loop through each file
for i = 1:length(Files)
% Construct the full file name and load the .mat file
fn = fullfile(Files(i).folder, Files(i).name);
load(fn);
% Assuming 'val' is the variable containing the signal
y = val(1:3600);
% Denoise the signal
[thr, sorh, keepapp] = ddencmp('den', 'wv', y);
sig1 = wdencmp('gbl', y, 'sym10', 10, thr, sorh, keepapp);
% Define sampling frequency
fs = 128;
% Generate and display the spectrogram
figure('Visible', 'off'); % Create a figure without displaying it
pspectrum(sig1, fs, 'spectrogram', 'TimeResolution', 0.5);
title(['Spectrogram of Signal ', num2str(i)]);
% Save the spectrogram as a JPG file
temp = fullfile('Spectrograms', [num2str(i, '%04d'), '.jpg']);
saveas(gcf, temp);
close(gcf); % Close the figure after saving
end
For more details, please refer to the following MathWorks documentation:
Hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Time-Frequency Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!